2016-10-04 159 views
-5

我正在做一個測試程序。將會有2個輸入,第一個數字和第二個數字。如果第一個數字大於或小於第一個數字小於第二個數字,請檢查第一個數字。用戶將再次輸入「輸入第二個數字」,我不知道該怎麼做,而在那裏。感謝檢查第二個數字是否大於或小於第一個數字

這是我的代碼:

#include<stdio.h> 

int main() 
{ 
    int number1,number2,total; 
    printf("Enter first number"); 
    scanf("%d",&number1); 
    printf("Enter second number"); 
    scanf("%d",&number2); 
    if(number1 > number2) 
    { 
     total = number1 - number2; 
     printf("%d",total);   
    } 
    else 
    { 
     printf("Number 1 is less than to number 2"); 
    } 

    return 0; 

} 
+1

很難理解什麼是你的問題。 –

+0

我編輯了我的問題 – Blacksmith

+0

您問我們如何在代碼中做一段時間循環來讀取用戶輸入? [您是否嘗試過使用Google搜索?](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=read+user+input+in+a+loop+c ) –

回答

0
#include <stdio.h> 

int main(void){ 
    int number1, number2, total; 

    printf("Enter first number >"); 
    scanf("%d", &number1); 

    while(1){ 
     printf("Enter second number >"); 
     int status = scanf("%d", &number2);//check return value of scanf 
     if(status == 1){// read integer but No check such as 3.5, 3? 
      if(number1 < number2) 
       printf("Number 1 is less than to number 2\n"); 
      else 
       break; 
     } else if(status == 0){//can't read integer 
      printf("invalid input.\n"); 
      scanf("%*[^\n]");scanf("%*c");//clear up to newline 
     } else {//if(status == EOF){//you want finish ;-) 
      puts("bye"); 
      return -1; 
     } 
    } 

    total = number1 - number2; 
    printf("%d\n", total);  

    return 0; 
} 
相關問題