我正在尝试设置一个程序,它将根据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 Studio 2012。
答案 0 :(得分:2)
您的开关案例适用于char
值。请注意,您的案例为case '1':
,case '2':
,...
因此,您可以将案例陈述从case '1':
更改为case 1:
或者
将week_day
变量的数据类型更改为char
。即。
int week_day;
至char week_day;
。
更改week_day
变量的数据类型后,请确保在scanf
语句中进行更改。
将scanf("%d", &week_day);
更改为scanf(" %c", &week_day);