2017-09-12 24 views
1
public class child { 
    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     int AGE; 
     System.out.println("Enter Child's number(s) of Days: "); 
     AGE = scan.nextInt(); 

     if (AGE == 0 || AGE == 1){ 
      System.out.println("Classification: New Born"); 
     } 
     else if (AGE >= 2 && AGE <= 10){ 
      System.out.println("Classification: Infant"); 
     } 
     else if (AGE > 9 && AGE < 19){ 
      System.out.println("Classification: New Born"); 
     } 
     else if (AGE > 17 && AGE < 37){ 
      System.out.println("Classification: TODDLER"); 
     } 
     else if (AGE > 36 && AGE < 120) { 
      System.out.println("Classification: KID"); 
     } 
     else{ 
      System.out.println("Classification: Out of Range"); 
      System.out.println("Enter a number again:"); 
      AGE = scan.nextInt(); 
      if (AGE == 0 || AGE == 1){ 
       System.out.println("Classification: New Born"); 
      } 
      else if (AGE >= 2 && AGE <= 10){ 
       System.out.println("Classification: Infant"); 
      } 
      else if (AGE > 9 && AGE < 19){ 
       System.out.println("Classification: New Born"); 
      } 
      else if (AGE > 17 && AGE < 37){ 
       System.out.println("Classification: TODDLER"); 
      } 
      else if (AGE > 36 && AGE < 120) { 
       System.out.println("Classification: KID"); 
      }   
      else{ 
       System.out.println("Classification: Out of Range"); 
       System.out.println("Enter a number again:"); 
       AGE = scan.nextInt(); 
      } 
     } 
    } 
} 

因此,我根據他們的天數對孩子進行了分類。如何讓程序不斷提示用戶?

如何簡化此代碼,因爲如果用戶輸入超出範圍的數字,程序將提示用戶輸入一個值,但如果用戶再次輸入一個超出範圍的數字現在將停止提示用戶輸入另一個號碼,因爲如果我把另一個條件結構放在其他地方,它會使我的代碼更長,所以我的問題是如何縮短我的代碼,但如果輸入的號碼程序將繼續提示用戶超出範圍。

樣本輸出 輸入孩子的天數(S):分類:超出範圍 再次輸入一個數字: //程序會提示用戶重新 -1 //但現在的程序現在不會打印出的範圍 /我如何使程序提示用戶再次輸入一個數字,讓我 短節目/

+1

使用'while'循環。 –

+1

使用'while'循環 –

回答

7

你應該熟悉一個while循環。

只要條件在每次迭代後保持爲真,它就會循環。

Scanner scan = new Scanner(System.in); 

while(true) { 
    System.out.println("Enter Child's number(s) of Days: "); 
    int AGE = scan.nextInt(); 

    if (AGE == 0 || AGE == 1) { 
     System.out.println("Classification: New Born"); 
    } else if (AGE >= 2 && AGE <= 10) { 
     System.out.println("Classification: Infant"); 
    } else if (AGE > 9 && AGE < 19) { 
     System.out.println("Classification: New Born"); 
    } else if (AGE > 17 && AGE < 37) { 
     System.out.println("Classification: TODDLER"); 
    } else if (AGE > 36 && AGE < 120) { 
     System.out.println("Classification: KID"); 
    } else { 
     System.out.println("Classification: Out of Range"); 
    } 
} 

在附註中,Java約定是用大寫字母(child - > Child)啓動類名。

+0

哦謝謝,所以我可以結合循環和條件構造。非常感謝 –

1
public class child { 
    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     int AGE; 
     while(true){ 
      System.out.println("Enter Child's number(s) of Days: "); 
      try { 
       AGE = scan.nextInt(); 
      } catch (Exception e) { 
       System.out.println ("Please enter a number!"); 
      }    

      if (AGE == 0 || AGE == 1){ 
       System.out.println("Classification: New Born"); 
      } 
      else if (AGE >= 2 && AGE <= 10){ 
       System.out.println("Classification: Infant"); 
      } 
      else if (AGE > 9 && AGE < 19){ 
       System.out.println("Classification: New Born"); 
      } 
      else if (AGE > 17 && AGE < 37){ 
       System.out.println("Classification: TODDLER"); 
      } 
      else if (AGE > 36 && AGE < 120) { 
       System.out.println("Classification: KID"); 
      } 
      else{ 
       System.out.println("Classification: Out of Range"); 
      } 
     } 
    } 
} 

希望這會有所幫助。

相關問題