2015-09-28 23 views
0

在試用此程序的這一部分時,我得到了Xcode中的「代碼將永遠不會執行」警告。該警告出現在do while循環的while部分的條件中。如果列表中顯示的其中一個數字沒有輸入,應打印下面的字符串。有什麼建議麼?在執行循環條件時出錯C

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 


int main() 
{ 

int input; 

    do 
    { 
     printf("Welcome to the Arithmetic Quiz Program!\n\n 
       Please choose one of the following by entering the correspoding number:\n\n 
       1. Give me an addition problem.\n 
       2. Give me a subtraction problem.\n 
       3. Give me a multiplication problem.\n 
       4. Quit.\n\n"); 

     scanf(" %i", &input); 

    } while (input!=1 || input!=2 || input!=3 || input!=4); 

return 0; 

} 
+1

使用'在while循環中&&'而不是'||'。 – Himanshu

+2

'input!= 1 ||輸入!= 2 ||輸入!= 3 ||輸入!= 4'永遠不會是錯誤的。要明白爲什麼,請嘗試在您的腦海中進行評估。 – immibis

+0

do-while循環至少執行一次。 – XDProgrammer

回答

0

如果您do while循環條件下具有的所有可能的條件,那麼它 將永遠不會計算爲假突破循環

OR條件只需要一個條件評估爲True,所以如果用戶輸入是1

Input!=1 //evaluates to false 
Input!=2 //becomes True 

等等

使用& &運營商,這將檢查,如果你的輸入沒有1,2,3,4這將打破

0
int main() 
{ 

int input; 

    do 
    { 
     printf("Welcome to the Arithmetic Quiz Program!\n\n 
       Please choose one of the following by entering the correspoding number:\n\n 
       1. Give me an addition problem.\n 
       2. Give me a subtraction problem.\n 
       3. Give me a multiplication problem.\n 
       4. Quit.\n\n"); 

     scanf(" %i", &input); 

    } while (input!=1 && input!=2 && input!=3 && input!=4); 

return 0; 

} 

雖然不是1也不是2也不3或4會再次重複