2016-05-26 197 views
2

我遇到問題。代碼應該採用兩位數字,其中數字不同。如果它不是整數,則代碼會告訴您並要求重新輸入。如果它是整數但不符合條件,它會告訴你,然後提示重新輸入。除了當您輸入0來結束循環/程序時,所有內容都可以使用。我已經堅持了2天。有什麼建議麼?範圍和輸入類型驗證

int num = 1; 
    while (num != 0) { 
     System.out.print("Enter a 2-digit number. The digits should be different. Zero to stop: "); 
     while (!in.hasNextInt()) { 
      System.out.print("Not an integer, try again: "); 
      in.next(); 
     } 
     num = in.nextInt(); 
     while (num < 10 || num > 99) { 
      System.out.println("NOT good for your game!"); 
      System.out.print("Enter a 2-digit number. The digits should be different. Zero to stop: "); 
      while (!in.hasNextInt()) { 
       System.out.print("Not an integer, try again: "); 
       in.next(); 
      } 
      num = in.nextInt(); 
     } 
     if (equalDigs(num) == false) 
      System.out.println("NOT good for you game!"); 
     else 
      System.out.println("Good for your game! Play!"); 

    } 

} 
public static boolean equalDigs(int n) { 
    int d1 = n/10; 
    int d2 = n % 10; 

    if (d1 == d2) 
     return false; 
    else 
     return true; 
} 
+1

您應該對此用例使用do-while。試試吧,它會高度簡化問題。 –

回答

0

您的問題來自重複檢查。您可以使用do-while語句而不是一個while聲明,以減少檢查次數:

public static void main(String[] args) throws Exception { 
    Scanner in = new Scanner(System.in); 
    int num; 
    do { 
    System.out.print("Enter a 2-digit number. The digits should be different. Zero to stop: "); 
    String input = in.next(); 
    try { 
     num = Integer.valueOf(input); 
     if(num == 0) { 
     break; 
     } 
     if(num < 10 || num > 99) { 
     System.out.println("NOT good for your game!"); 
     } else{ 
     if(input.charAt(0) == input.charAt(1)) { 
      System.out.println("NOT good for you game!"); 
     } else { 
      System.out.println("Good for your game! Play!"); 
     } 
     } 
    } catch (NumberFormatException ignored) { 
     System.out.print("Not an integer, try again: "); 
    } 
    } while (true); 
} 
+0

欣賞它,但沒有例外嗎?這是一個介紹課,我們還沒有完成所有這些。 –

0

一種方式重構你的代碼將在命令行,而不是閱讀數String閱讀,然後驗證該輸入,以確保它符合下列條件:

  • 輸入長度爲兩個字符
  • 兩個字符是數字
  • 第一和第二個數字是唯一的

我創建了一個名爲validate()的方法,它爲此做了繁重的工作。這使得您的main()方法只關注輪詢用戶輸入,只要驗證失敗。

public static boolean validate(String input) { 
    // check that input length is two characters 
    if (input.length() != 2) { 
     return false; 
    } 
    // check that a two digit number was entered 
    if (Character.getNumericValue(input.charAt(0)) < 0 || Character.getNumericValue(input.charAt(0)) > 9 || 
     Character.getNumericValue(input.charAt(0)) < 0 || Character.getNumericValue(input.charAt(0)) > 9) { 
     return false; 
    } 
    // check that first and second numbers are unique 
    if (input.charAt(0) == input.charAt(1)) { 
     return false; 
    } 

    return true; 
} 

public static void main(String[] args) { 
    Scanner reader = new Scanner(System.in); 
    String input; 
    do { 
     System.out.println("Enter a 2-digit number. The digits should be different. Zero to stop: "); 
     input = reader.next(); 
     if (!validate(input)) { 
      System.out.println("NOT good for your game!"); 
     } 
     else { 
      break; 
     } 
    } while(true); 

    System.out.println("Good for your game! Play!"); 
    // you can also use the value of 'input' here if needed 
} 
+0

欣賞,但它可以做到沒有字符串和.getNumericalValue,因爲我們還沒有學習.getNu ...,你應該輸入int的字符串 –

+0

mg解決方案的優點是,用戶可以輸入他想要的任何東西。如果輸入是非數字的,代碼會簡單地拒絕它並再次詢問。 –