2013-12-13 62 views
-1

如何在此程序中添加循環以便程序保持執行,然後在用戶輸入介於100和999之間的有效數字時結束?如何添加一個循環到這個程序並讓它保持運行直到程序爲真?

import java.util.*; 

public class numLoop{ 
public static void main(String[] arg){ 
    Scanner sc = new Scanner(System.in); 
    int number = 0; 

    try{ 
    Scanner reader = new Scanner(System.in); 
    System.out.print("Enter a number between 100 and 999: "); 
    number = reader.nextInt(); 

    if(number > 999 || number < 100) 
     System.out.println("\nYour number was invalid."); 

    else 
     System.out.println("\nThe number " + number + " was vaild."); 

     } 
     catch(InputMismatchException ime){ 
      System.out.println("You didn't enter a number."); 

    } 
} 
} 
+0

爲做好搜索/而結構。 –

+0

您正在尋找[while循環](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)。 – thegrinner

+0

實現'while'循環 –

回答

1

把你整個的try/catch一個while循環中:

while (number < 100 || number > 999) 
{ 
    // ... 
} 

或者使用while (true) { }for (;;) { }把它放在一個無限循環,然後添加一個條件break如果輸入一個有效的數字:

if (number < 100 || number > 999) 
{ 
    // handle invalid entry 
} 

else 
{ 
    // handle valid entry 

    break; // exit loop 
} 
+0

請,不爲(;;):) –

+0

無需重複檢查的數量 使用,而(真),並突破是一個非常不好的做法 –

+0

@AndreaGirardi爲何+有效性?這一直是我最喜歡的無限循環風格,幾周前我剛剛和同事談了一次,所以我很好奇你的理由是什麼:) – TypeIA

1

使用while循環可以實現以下邏輯

(僞)

Input a number 
while (number is not valid) 
    Tell them something went wrong 
    Input another number 
Valid number was entered, do something about it 

while循環本身看起來像

while (condition) { 
    // code to run while condition is true 
} 
0
import java.util.*; 

public class numLoop{ 
public static void main(String[] arg){ 
    Scanner sc = new Scanner(System.in); 
    int number = 0; 

    try{ 
    Scanner reader = new Scanner(System.in); 
    System.out.print("Enter a number between 100 and 999: "); 
    number = reader.nextInt(); 

    while(number > 999 || number < 100){ 
     System.out.println("\nYour number was invalid."); 
     number = reader.nextInt(); 
    } 

    System.out.println("\nThe number " + number + " was vaild."); 


     }catch(InputMismatchException ime){ 
      System.out.println("You didn't enter a number."); 

    } 
} 
} 
+0

這裏有一些編譯錯誤!在捕捉之前缺少一個},當然,不確定它實際上是否是提交的問題 –

+0

的答案,我回答得太快,因而錯放了一個大括號 –

0

您還可以使用一段時間(真)中斷循環是這樣的:

import java.util.*; 

    public class numLoop{ 
    public static void main(String[] arg){ 
     Scanner sc = new Scanner(System.in); 
     int number = 0; 
    while(true){ 
     try{ 
     Scanner reader = new Scanner(System.in); 
     System.out.print("Enter a number between 100 and 999: "); 
     number = reader.nextInt(); 

     if(number > 999 || number < 100) 
      System.out.println("\nYour number was invalid."); 

     else 
      System.out.println("\nThe number " + number + " was vaild."); 
      break; 
      } 
      catch(InputMismatchException ime){ 
       System.out.println("You didn't enter a number."); 

     } 
    } 
    } 
    } 

被警告不是每個人都是這樣的事情,這是很好的代碼練習冰。

0

三個小行...您現有的try前添加

while(true) // you could (and should) add braces (for the look of it). 
      // could also have used for(;;) 

這裏

else 
    System.out.println("\nThe number " + number + " was vaild."); 

那麼你需要一個

else { 
    System.out.println("\nThe number " + number + " was vaild."); 
    break; // End the infinite loop. 
} 
相關問題