switch语句只返回默认大小写,无论输入什么大小写值
我正试图建立一个程序,根据用户输入的从1到7的数字来判断一周中的哪一天。但每次程序执行时,它都返回默认的case消息,表示输入了不正确的值。这适用于所有案例条目1-7。以下是程序的设置(不包括标题):switch语句只返回默认大小写,无论输入什么大小写值,c,switch-statement,C,Switch Statement,我正试图建立一个程序,根据用户输入的从1到7的数字来判断一周中的哪一天。但每次程序执行时,它都返回默认的case消息,表示输入了不正确的值。这适用于所有案例条目1-7。以下是程序的设置(不包括标题): //作者:伊桑·亚当斯 //日期:2014年9月10日 //目的:根据用户输入值确定星期几 #包括 #包括 内部主(空) { int week_day;//用户输入的值 printf(“输入一周的天数(1-7):\n”);//提示 scanf(“%d”,&week\u day);//输入值 开关(
//作者:伊桑·亚当斯
//日期:2014年9月10日
//目的:根据用户输入值确定星期几
#包括
#包括
内部主(空)
{
int week_day;//用户输入的值
printf(“输入一周的天数(1-7):\n”);//提示
scanf(“%d”,&week\u day);//输入值
开关(周/日){
案例“1”:
printf(“一周中的一天是星期天。\n”);
break;//退出开关
案例“2”:
printf(“星期一是星期一。\n”);
break;//退出开关
案例“3”:
printf(“一周中的一天是星期二。\n”);
break;//退出开关
案例“4”:
printf(“一周中的一天是星期三。\n”);
break;//退出开关
案例“5”:
printf(“一周中的一天是星期四。\n”);
break;//退出开关
案例“6”:
printf(“一周中的一天是星期五。\n”);
break;//退出开关
案例“7”:
printf(“一周中的一天是星期六。\n”);
break;//退出开关
违约:
printf(“输入的值不正确。请重试。\n”);
break;//退出开关
}//终端开关选择
系统(“暂停”);
}//端干管
另外,如果Visual Studio 2012有任何影响的话,我会使用它。您的开关盒用于
char
值。请注意,您的案例有案例“1”:
,案例“2”:
因此,您可以将case语句从case'1':
更改为case 1:
或
将week\u day
变量的数据类型更改为char
。i、 e
int week\u day代码>至字符周\天代码>
更改week\u day
变量的数据类型后,请确保在scanf
语句中进行更改
更改scanf(“%d”周/日)代码>至scanf(“%c”和周/日)代码>
//author: Ethan Adams
//date: 10/09/14
//purpose: to determine the day of the week based on user input value
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int week_day; //value entered by user
printf("Enter the number of the week's day (1-7):\n");//prompt
scanf("%d", &week_day); //input value
switch(week_day){
case '1':
printf("The day of the week is Sunday.\n");
break;//exit switch
case '2':
printf("The day of the week is Monday.\n");
break;//exit switch
case '3':
printf("The day of the week is Tuesday.\n");
break;//exit switch
case '4':
printf("The day of the week is Wednesday.\n");
break;//exit switch
case '5':
printf("The day of the week is Thursday.\n");
break;//exit switch
case '6':
printf("The day of the week is Friday.\n");
break;//exit switch
case '7':
printf("The day of the week is Saturday.\n");
break;//exit switch
default:
printf("Improper value entered. Please try again.\n");
break;//exit switch
}//end switch selection
system ("pause");
}//end main