为什么逻辑运算符不按预期工作? [英] Why the logical operators are not working as expected?

查看:144
本文介绍了为什么逻辑运算符不按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者CS当然,我试图用C编写一个程序,要求两个数字,并返回该数字的写出来的形式。我写的所有code和它的作品时,我尝试了数10-19,但没有任何其他人。采用C89标准如果该事项已编译

 的#include<&stdio.h中GT;INT主要(无效)
{的printf(\\ n个转换为Word程序);
的printf(\\ n \\ n这个项目需要一个两位数字输出的英文单词为数字);INT数= 0;
INT numberH1 = 0;
INT numberH2 = 0;的printf(\\ n \\ n请输入两个数字:);
scanf函数(%d个,&安培;号码);numberH1 =号/ 10;
numberH2 =编号%10;如果(数字== || 10 || 11 || 12 || 13 || 14 || 15 || 16 || 17 || 18 19)
{
    开关(数)
    {
        案例10:
        {
            的printf(\\ n \\ n输入的号码十大\\ n \\ n);
        }
        打破;
        案例11:
        {
            的printf(\\ n \\ n输入的号码十一\\ n \\ n);
        }
        打破;
        案例12:
        {
            的printf(\\ n \\ n输入的号码十二\\ n \\ n);
        }
        打破;
        案例13:
        {
            的printf(\\ n \\ n输入的号码十三\\ n \\ n);
        }
        打破;
        案例14:
        {
            的printf(\\ n \\ n输入的号码十四\\ n \\ n);
        }
        打破;
        案例15:
        {
            的printf(\\ n \\ n输入的号码十五\\ n \\ n);
        }
        打破;
        案例16:
        {
            的printf(\\ n \\ n输入的号码十六\\ n \\ n);
        }
        打破;
        案例17:
        {
            的printf(\\ n \\ n输入的号码十七\\ n \\ n);
        }
        打破;
        案例18:
        {
            的printf(\\ n \\ n输入的号码十八\\ n \\ n);
        }
        打破;
        案例19:
        {
            的printf(\\ n \\ n输入的号码十九\\ n \\ n);
        }
        打破;
    }
}
其他
{开关(numberH1)
{
    案例2:
    {
        的printf(\\ n \\ n最NUMER进入二十);
    }
    打破;
    案例3:
    {
        的printf(\\ n \\ n最NUMER进入三十);
    }
    打破;
    情况4:
    {
        的printf(\\ n \\ n最NUMER进入四十);
    }
    打破;
    情况5:
    {
        的printf(\\ n \\ n最NUMER进入五十);
    }
    打破;
    情况6:
    {
        的printf(\\ n \\ n最NUMER进入六十);
    }
    打破;
    案例7:
    {
        的printf(\\ n \\ n最NUMER进入七十);
    }
    打破;
    案例8:
    {
        的printf(\\ n \\ n最NUMER进入八十);
    }
    打破;
    案例9:
    {
        的printf(\\ n \\ n最NUMER进入九十);
    }
    打破;
}开关(numberH2)
{
    情况下0:
    {
        的printf(\\ n \\ n);
    }
    打破;
    情况1:
    {
        输出( - 一个\\ n \\ n);
    }
    打破;
    案例2:
    {
        输出( - 两个\\ n \\ n);
    }
    打破;
    案例3:
    {
        输出( - 三\\ n \\ n);
    }
    打破;
    情况4:
    {
        输出( - 四\\ n \\ n);
    }
    打破;
    情况5:
    {
        输出( - 五个\\ n \\ n);
    }
    打破;
    情况6:
    {
        输出( - 六\\ n \\ n);
    }
    打破;
    案例7:
    {
        输出( - SEVEN \\ n \\ n);
    }
    打破;
    案例8:
    {
        输出( - 八个\\ n \\ n);
    }
    打破;
    案例9:
    {
        的printf( - 9 \\ n \\ n);
    }
    打破;
}
}返回0;}


解决方案

在您的code,

 如果(数字== || 10 || 11 || 12 || 13 || 14 || 15 || 16 || 17 || 18 19)

是一个错误的做法。你不能链中的逻辑运算符这样的。

,因为运营商$ P的截至目前, $ pcedence ,你的code基本上

  IF((数== 10)|| 11 || 12 || 13 || 14 || 15 || 16 || 17 || 18 || 19)

计算结果为

或者


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


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

这两者将产生真正的价值。

您必须使用它像

  IF((数== 10)||(数== 11)||(数== 12).....

我所看到的,你已经在使用开关声明,因此如果检查可以去除共。您需要添加一个默认情况下处理的其他的数字。您可以添加嵌套的开关来得到事情做好。

I am in a beginner CS course and I am trying to write a program in C that asks for a two digit number and returns the written out form of that number. I have written all of the code and it works when I try the numbers 10-19 but not any others. Compiled using C89 standard if that matters

#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;

}

解决方案

In your code,

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

is a wrong approach. You cannot chain the logical operator like that.

As of now, because of the operator precedence, your code is essentially

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

which evaluates to either

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

or

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

both of which will yield a TRUE value.

You have to use it like

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

As I can see, you're already using the switch statement, so the if check can be removed altogether. You need to add a default case to handle other numbers. You can add nested switch to get the things done.

这篇关于为什么逻辑运算符不按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆