【发布时间】:2024-01-20 23:40:01
【问题描述】:
我有一个奇怪的问题。尝试使用 if 语句和 do-while 循环来验证我的条件,以防 char 变量不等于 'c' 和 'f' 但是当我选择了 'c' 或 'C' 或 'f' 或'F' 在不起作用的部分之后仍然显示“错误选择”。当我输入“c”时,我跟踪打印变量(正如您可以看到下面注释的printf() 代码)我可以看到程序接受了“c”但仍显示为错误输入。
我已经打电话给#include<stdio.h> 和#include<ctype.h>。
谁能帮帮我?
char choice;
do
{
printf("\nChoose between Celsius (c) or for Fahrenheit (f):");
scanf(" %c*%c", &choice);
//printf("\nchoice1: %c", choice);
choice = tolower(choice);
//printf("\nchoice2: %c", choice);
if(choice != 'c' || choice != 'f')
printf("\nWrong choice!!");
}while(choice!='c' || choice!='f');
【问题讨论】:
标签: c if-statement char scanf