gpt4 book ai didi

c - 为什么逻辑运算符没有按预期工作?

转载 作者:行者123 更新时间:2023-11-30 21:42:41 24 4
gpt4 key购买 nike

我正在学习初级计算机科学类(class),我正在尝试用 C 语言编写一个程序,该程序要求输入两位数并返回该数字的写出形式。我已经编写了所有代码,当我尝试数字 10-19 但不尝试任何其他数字时,它可以工作。如果重要的话,使用 C89 标准编译

#include <stdio.h>

int main(void)
{

printf("\n Number to Word Conversion Program");
printf("\n\n This program takes a two-digit number and outputs the English word for the number");

int number = 0;
int numberH1 = 0;
int numberH2 = 0;

printf("\n\n Please enter a two-digit number: ");
scanf("%d", &number);

numberH1 = number / 10;
numberH2 = number % 10;

if (number == 10 || 11 || 12 || 13 || 14 || 15 || 16 || 17 ||18 || 19)
{
switch (number)
{
case 10:
{
printf("\n\n The number entered was Ten.\n\n");
}
break;
case 11:
{
printf("\n\n The number entered was Eleven.\n\n");
}
break;
case 12:
{
printf("\n\n The number entered was Twelve.\n\n");
}
break;
case 13:
{
printf("\n\n The number entered was Thirteen.\n\n");
}
break;
case 14:
{
printf("\n\n The number entered was Fourteen.\n\n");
}
break;
case 15:
{
printf("\n\n The number entered was Fifteen.\n\n");
}
break;
case 16:
{
printf("\n\n The number entered was Sixteen.\n\n");
}
break;
case 17:
{
printf("\n\n The number entered was Seventeen.\n\n");
}
break;
case 18:
{
printf("\n\n The number entered was Eighteen.\n\n");
}
break;
case 19:
{
printf("\n\n The number entered was Nineteen.\n\n");
}
break;
}
}
else
{

switch (numberH1)
{
case 2:
{
printf("\n\n The numer entered was Twenty");
}
break;
case 3:
{
printf("\n\n The numer entered was Thirty");
}
break;
case 4:
{
printf("\n\n The numer entered was Forty");
}
break;
case 5:
{
printf("\n\n The numer entered was Fifty");
}
break;
case 6:
{
printf("\n\n The numer entered was Sixty");
}
break;
case 7:
{
printf("\n\n The numer entered was Seventy");
}
break;
case 8:
{
printf("\n\n The numer entered was Eighty");
}
break;
case 9:
{
printf("\n\n The numer entered was Ninety");
}
break;
}



switch (numberH2)
{
case 0:
{
printf(".\n\n");
}
break;
case 1:
{
printf("-one.\n\n");
}
break;
case 2:
{
printf("-two.\n\n");
}
break;
case 3:
{
printf("-three.\n\n");
}
break;
case 4:
{
printf("-four.\n\n");
}
break;
case 5:
{
printf("-five.\n\n");
}
break;
case 6:
{
printf("-six.\n\n");
}
break;
case 7:
{
printf("-seven.\n\n");
}
break;
case 8:
{
printf("-eight.\n\n");
}
break;
case 9:
{
printf("-nine.\n\n");
}
break;
}
}

return 0;

}

最佳答案

在您的代码中,

 if (number == 10 || 11 || 12 || 13 || 14 || 15 || 16 || 17 ||18 || 19)

是一种错误的做法。您不能像这样链接逻辑运算符。

截至目前,由于 operator precedence ,你的代码本质上是

if ( (number == 10) || 11 || 12 || 13 || 14 || 15 || 16 || 17 ||18 || 19)

其计算结果为:

  • if ( 1 || 11 || 12 || 13 || 14 || 15 || 16 || 17 ||18 || 19)

  • if ( 0 || 11 || 12 || 13 || 14 || 15 || 16 || 17 ||18 || 19)

两者都会产生一个 TRUE 值。

你必须像这样使用它

if ((number == 10) || (number ==  11)||(number ==  12).....

正如我所见,您已经在使用 switch 语句,因此可以完全删除 if 检查。您需要添加一个default case 来处理其他号码。您可以添加嵌套switch来完成任务。

关于c - 为什么逻辑运算符没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35437589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com