2013-04-05 86 views
4

我需要讓用戶輸入一個數字作爲範圍的開始,然後輸入另一個數字,即範圍的結尾。起始數字必須爲0或更大,結束數字不能大於1000.兩個數字都必須可以被10整除。我找到了一種方法來滿足這些條件,但是如果它們不符合我的程序只是告訴用戶他們的輸入不正確。是否可以對其進行編碼,以便在用戶輸入之後將檢查以確保滿足條件,並且如果它們沒有回送並再次輸入它們。這是我迄今爲止的代碼。循環用戶輸入,直到條件滿足

Scanner keyboard = new Scanner(System.in); 
    int startr; 
    int endr; 
    System.out.println("Enter the Starting Number of the Range: "); 
    startr=keyboard.nextInt(); 
    if(startr%10==0&&startr>=0){ 
     System.out.println("Enter the Ending Number of the Range: "); 
     endr=keyboard.nextInt(); 
     if(endr%10==0&&endr<=1000){ 

     }else{ 
      System.out.println("Numbers is not divisible by 10"); 
     } 
    }else{ 
     System.out.println("Numbers is not divisible by 10"); 
    } 

回答

6

易與DO-同時:

Scanner keyboard = new Scanner(System.in); 
int startr, endr; 
boolean good = false; 
do 
{ 
    System.out.println("Enter the Starting Number of the Range: "); 
    startr = keyboard.nextInt(); 
    if(startr % 10 == 0 && startr >= 0) 
    good = true; 
    else 
    System.out.println("Numbers is not divisible by 10"); 
} 
while (!good); 

good = false; 
do 
{ 
    System.out.println("Enter the Ending Number of the Range: "); 
    endr = keyboard.nextInt(); 
    if(endr % 10 == 0 && endr <= 1000) 
     good = true; 
    else 
     System.out.println("Numbers is not divisible by 10"); 
} 
while (!good); 

// do stuff 
+0

只是一個快速跟進問題,while語句中的(!good)是在說什麼?而好=原始值? – Student 2013-04-05 21:09:18

+0

'good'意思是'good == true','!good'意思是'good == false'(但'==布爾條件'通常是不好的做法)。 – Dukeling 2013-04-05 21:11:09

+0

這會在'nextInt()'語句中拋出'IllegalStateException'。 – user3932000 2016-10-18 23:17:52

3

您需要使用時,是這樣的:

while conditionsMet is false 
    // gather input and verify 
    if user input valid then 
     conditionsMet = true; 
end loop 

應該這樣做。

+0

我建議讓代碼更接近Java - 'while(!conditionsMet)','if(/ * user input valid * /)',no'then'並且沒有'end loop'。 '{'和'}'可選。 – Dukeling 2013-04-05 20:52:13

0

全目的的程序是:

  1. 讀輸入中無限循環。
  2. 當條件滿足時,使用break;語句退出循環。

實施例:

Scanner keyboard = new Scanner(System.in); 
int startr, endr; 

for (;;) { 
    System.out.println("Enter the starting number of the range: "); 
    startr = keyboard.nextInt(); 
    if (startr >= 0 && startr % 10 == 0) break; 
    System.out.println("Number must be >= 0 and divisible by 10."); 
} 

for (;;) { 
    System.out.println("Enter the ending number of the range: "); 
    endr = keyboard.nextInt(); 
    if (endr <= 1000 && endr % 10 == 0) break; 
    System.out.println("Number must be <= 1000 and divisible by 10."); 
} 

如果無效輸入之後要,而無需重複初始提示信息只顯示該錯誤消息,移動至初始提示消息的正上方/外循環。

如果沒有需要單獨的錯誤信息,則可以重新安排代碼以使用do-while循環檢查的條件,這僅僅是一個短一點:

Scanner keyboard = new Scanner(System.in); 
int startr, endr; 

do { 
    System.out.println("Enter the starting number of the range."); 
    System.out.println("Number must be >= 0 and divisible by 10: "); 
    startr = keyboard.nextInt(); 
} while (!(startr >= 0 && startr % 10 == 0)); 

do { 
    System.out.println("Enter the ending number of the range."); 
    System.out.println("Number must be <= 1000 and divisible by 10: "); 
    endr = keyboard.nextInt(); 
} while (!(endr <= 1000 && endr % 10 == 0));