我正在尝试建立一个程序,它将根据用户输入的数字从1到7来判断一周中的哪一天。不过,每次程序执行时,它都会返回默认的大小写消息,表示输入了一个不正确的值。这发生在所有的案例条目1-7。下面是程序的设置(不包括头):
//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
此外,如果有任何影响,我将使用Visual 2012。
发布于 2014-10-11 11:04:26
您的开关大小写用于char
值。注意你有case '1':
,case '2':
,.
因此,您可以将case语句从case '1':
更改为case 1:
。
或
将week_day
变量的数据类型更改为char
。即
int week_day;
到char week_day;
。
更改week_day
变量的数据类型后,请确保对scanf
语句进行更改。
将scanf("%d", &week_day);
更改为scanf(" %c", &week_day);
https://stackoverflow.com/questions/26318073
复制相似问题