读书人

如其不用switch而直接用if写这个代码

发布时间: 2013-10-22 16:16:51 作者: rapoo

如果不用switch而直接用if写这个代码
在屏幕上显示一张如下所示的时间表:
*****Time*****
1 morning
2 afternoon
3 night
Please enter your choice:

操作人员根据提示进行选择,程序根据输入的时间序号显示相应的问候信息,选择1时显示"Good morning", 选择2时显示"Good afternoon", 选择3时显示"Good night",对于其他选择显示"Selection error!"。


如题 ,各位哥哥姐姐,如何直接用if写啊?
#include <stdio.h>
main()
{
int choice;
printf("*****Time*****\n1 morning\n2 afternoon\n3 night\nPlease enter your choice:");
scanf("%d",&choice);
if(choice=1)
{
printf("good morning\n");
}
if(choice=2)
{
printf("good afternoon\n");
}
}
像这样写的话,无论输入什么都会显示good mornin和good afternoon。
谢谢你们,因为我是新手,没有很多分给你们,希望你们还是能帮帮忙 switch if 新手求教
[解决办法]

引用:
在屏幕上显示一张如下所示的时间表:
*****Time*****
1 morning
2 afternoon
3 night
Please enter your choice:

操作人员根据提示进行选择,程序根据输入的时间序号显示相应的问候信息,选择1时显示"Good morning", 选择2时显示"Good afternoon", 选择3时显示"Good night",对于其他选择显示"Selection error!"。


如题 ,各位哥哥姐姐,如何直接用if写啊?
#include <stdio.h>
main()
{
int choice;
printf("*****Time*****\n1 morning\n2 afternoon\n3 night\nPlease enter your choice:");
scanf("%d",&choice);
if(choice=1)
{
printf("good morning\n");
}
if(choice=2)
{
printf("good afternoon\n");
}
}
像这样写的话,无论输入什么都会显示good mornin和good afternoon。
谢谢你们,因为我是新手,没有很多分给你们,希望你们还是能帮帮忙

你的判断语句写成了赋值语句,应该是choice==1,如果写成choice=1,则总是为真,所以一直执行第一个语句

读书人网 >C++

热点推荐