Tag: case

嵌套if或switch case语句

从优化代码运行时的角度来看,是否有一个拇指规则,用于何处使用“嵌套if”语句以及何时使用“switch case”语句?

案例标签在C中没有减少到整数常量?

我正在开发一个游戏,我运行了我的代码并得到错误“案例标签不会减少到一个整数常量。” 我想我知道这意味着什么,但我该如何解决呢? 这是我的代码: #include #include 0) { printf(“Please type A, B, C, Donate, Go to work, or Exit\n”); switch(jobs) { case ‘A’: player_cash[0]-=5; player_cash[1]+=5; printf(“Cash=%i\n\n”, player_cash[0]); continue; case ‘B’: player_cash[0]-=5; player_cash[2]+=5; printf(“Cash=%i\n\n”, player_cash[0]); continue; case ‘C’: player_cash[0]-=5; player_cash[3]+=5; printf(“Cash=%i\n\n”, player_cash[0]); continue; case “Donate”: player_cash[0]-=15; //Error here player_cash[1]+=5; player_cash[2]+=5; player_cash[3]+=5; printf(“Cash donated\n\n”); printf(“Cash=%i\n\n”, player_cash[0]); continue; case […]

C – 具有多个案例编号的开关

所以我的教授要求我们创建一个switch语句。 我们只允许使用“SWITCH”语句来执行该程序。 他希望我们输入一个数字然后显示它,如果它在数字范围内,将显示如下所示的公文包编号。 现在……我知道对于这种类型的程序,使用IF语句更容易。 做案例1:案例2:案例3 ……案例30将起作用但由于数字范围将花费太多时间。 #include main() { int x; char ch1; printf(“Enter a number: “); scanf(“%d”,&x); switch(x) { case 1://for the first case #1-30 case 30: printf(“The number you entered is >= 1 and = 31 and = 60 and = 90 and <= 100"); printf("\nTake Briefcase Number 4"); break; default: printf("Not in […]