首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >语句仅返回默认大小写,无论输入哪个大小写值。

语句仅返回默认大小写,无论输入哪个大小写值。
EN

Stack Overflow用户
提问于 2014-10-11 10:52:40
回答 1查看 183关注 0票数 1

我正在尝试建立一个程序,它将根据用户输入的数字从1到7来判断一周中的哪一天。不过,每次程序执行时,它都会返回默认的大小写消息,表示输入了一个不正确的值。这发生在所有的案例条目1-7。下面是程序的设置(不包括头):

代码语言:javascript
运行
AI代码解释
复制
//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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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);

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26318073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档