Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        4年前关闭。
                                                                                            
                
        
我正在参加CS入门课程,我试图用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大小写以处理其他数字。您可以添加嵌套的switch来完成任务。

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

10-11 07:48