【发布时间】:2021-11-24 09:06:30
【问题描述】:
我正在编写一个 C 程序,它要求用户提供各种输入,其中一个是“是”或“否”问题。如果输入 Y 或 y,则应该执行 If 语句。但是,无论您输入什么,它都会通过 If 语句。
double phonePrice;
double phoneTax;
double phoneTotal;
double phoneApplePrice;
double phoneSubtotal;
double temp;
int yearsAppleCare;
char userAnswer;
//prompting for price
printf("Enter the price of the phone> ");
scanf("%f", &phonePrice);
fflush(stdin);
//prompting for iphone
printf("Is the phone an iPhone (Y/N) ?> ");
scanf("%c", &userAnswer);
fflush(stdin);
//nested if statements asking for apple care amount
if(userAnswer=="Y"||"y")
{
printf("Enter the number of years of AppleCare> ");
scanf("%d", &yearsAppleCare);
if(yearsAppleCare<=0)
{
printf("You must choose at least 1 year of AppleCare");
return 0;
}
}
对此的任何帮助将不胜感激。
【问题讨论】:
-
userAnswer=="Y"||"y"应该是userAnswer=='Y' || userAnswer=='y'。 -
使用
if(userAnswer=='Y'||userAnswer=='y')。 -
不要使用
fflush(stdin):stackoverflow.com/questions/2979209/using-fflushstdin -
这很可能是我第一次看到这个问题(或其等效问题)要求使用 Python 以外的语言。奇怪的是,它实际上并没有更频繁地发生。
-
@KarlKnechtel 我已经看到并关闭了可能数十或一百个 C 和 C++ 问题,但我从未见过这样的 python 问题。可能是因为你在python问题上比较活跃
标签: c if-statement char scanf logical-or