所以我的教授要求我们创建一个switch语句。我们被允许使用只有“SWITCH”声明来执行该程序。他希望我们输入一个数字,然后在数字范围内显示,然后如下所示输入公文包号码。现在...我知道这种类型的程序更容易使用IF语句。做案例1:案例2:案例3 ...案例30将工作,但由于数量范围,将花费太多时间。C - 多个案例号的开关
#include <stdio.h>
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 <= 30");
printf("\nTake Briefcase Number 1");
break;
case 31://for the second case #31-59
case 59:
printf("The number you entered is >= 31 and <= 59");
printf("\nTake Briefcase Number 2");
break;
case 60://for the third case #60-89
case 89:
printf("The number you entered is >= 60 and <= 89");
printf("\nTake Briefcase Number 3");
break;
case 90://for the fourth case #90-100
case 100:
printf("The number you entered is >= 90 and <= 100");
printf("\nTake Briefcase Number 4");
break;
default:
printf("Not in the number range");
break;
}
getch();
}
我的教授告诉我们,如何做到这一点有一个简短的方法,但不会告诉我们如何。我能想到缩短它的唯一方法是使用IF,但我们不允许。任何想法,我如何可以使这项工作?
只是为了检查,你确定第一个案件从1到30?而不是1到29? –
您是否在考虑'案例1:案例30:'会匹配范围内的任何数字?它不会。 – John3136
您的第一对'case'标签只能找到2个值:1和30;他们忽略2..29。对于下一个“范围”也是如此,等等。只使用开关,你就会感受到一个痛苦的世界。 –