簡體   English   中英

C 程序未執行 Else 語句

[英]C Program Not Executing Else Statement

我的程序執行 if 語句但不執行 else 子句。 以下是代碼的條件: 對於每個 integer,n,在區間 [a,b] 中(作為輸入給出): 1 如果 1<=n<=9,則以小寫形式打印它的英文表示。 即“一”表示 1,“二”表示 2,依此類推。 2 如果 n>9 且為偶數,則打印“偶數” 3. 如果 n>9 且為奇數,則打印“奇數” "

第一個條件可以正常工作,但是當代碼到達 else 子句時,會顯示一個符號。

代碼:

    #include <stdio.h>

    #include <stdio.h>

    #include <string.h>

    #include <math.h>

    #include <stdlib.h>

    int main()

    {

    int a, b, n;

    char English[10][10]={"","one","two","three","four","five","six","seven","eight","nine"};

    scanf("%d\n%d", &a, &b);

    for(n=a;n<=b;n++)
    {
     if (1<=n<=9)
         printf("%s\n", English+n);

     else
        {
        if(n%2==0)
         printf("even\n");

        else
         printf("odd\n");
        }
}

return 0;
    }

輸入:

    8
    11

Output:

    eight
    nine
    ♂

預期 Output:

    eight
    nine
    even
    odd

它在 C 中不起作用。 它總是評估真相。 為什么? 1 <= n的結果是01 兩者總是小於9

if (1<=n<=9)

它應該是

     if (n >= 1 && n <= 9)
         printf("%s\n", English[n]);

https://godbolt.org/z/hQu4w9

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM