2016-10-09 32 views
1

我想驗證用戶輸入,以便程序將循環回到第一個詢問用戶的等級,如果它不是一個整數,並且int不在9-12範圍內。有沒有更好的方法來編寫這段代碼?有沒有辦法讓這個用戶驗證代碼更有效率和/或更容易閱讀?

do 
    { 
     if (userGrade < 9 || userGrade > 12) 
     { 
      System.out.println("That is not a valid grade!"); 
     } 

      System.out.printf("Grade (9-12): "); 

     while(!enterInfo.hasNextInt()) 
     { 
      System.out.println("That is not a number! Enter in a valid number."); 
      enterInfo.next(); 
     } 
     userGrade = enterInfo.nextInt(); 
    } while (userGrade < 9 || userGrade > 12); 
+0

while條件在do塊內重複出現。 – Stavm

+0

它看起來沒問題,除了在讀取任何需要調用'enterInfo.nextLine()'來消耗緩衝區中的換行符之後。 – Bohemian

+0

當答案有效時,您也可以使用'while(true)'循環和'break;'。 –

回答

0

基本上,我會讀System.in,雖然有一些東西,首先我會嘗試轉換爲整數,然後檢查是否該整數是在正確的範圍:

package trial; 

import java.util.Scanner; 

public class TestScan { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     try { 
      System.out.println("Please introduce a number:"); 
      Scanner sc = new Scanner(System.in); 
      while(sc.hasNext()){ 
       String input=sc.next(); 
       Integer inputInt; 
       try{ 
        inputInt=Integer.parseInt(input); 
       }catch(Exception e){ 
        System.out.println("You must introduce a number"); 
        continue; 
       } 
       if(inputInt<9 || inputInt>12){ 
        System.out.println("Number must be between 9 and 12 (inclusive)"); 
        continue; 
       } 
       System.out.println("Correct!"); 
      } 
      sc.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

} 

值得注意的是, ,因爲從System.in閱讀,這個程序可以從你的IDE執行的,所以你必須給你的IDE之外執行:

1-去TestScan文件夾,並編譯:javac的TestScan.java

2.-在你的classpatch中指定這個類來執行它。例如。如果你是在C:你可以使用類似

C:>的java -classpath C:\工作區\ StackOverflow上的\ src trial.TestScan

0
if (userGrade < 9 || userGrade > 12) 
    { 
     System.out.println("That is not a valid grade!"); 
    } else { 
    do { 
     System.out.printf("Grade (9-12):   
     "); 

    while(!enterInfo.hasNextInt()) 
    { 
     System.out.println("That is not a number! Enter in a valid number."); 
     enterInfo.next(); 
    } 
    userGrade = enterInfo.nextInt(); 
} while (userGrade < 9 || userGrade > 12)} 
1

爲了使基於代碼更加清晰,你可以用封裝類和 方法(封裝是順便說一句OOP的主要原因)。

因此,儘可能將所有的東西都分成小的部分作爲方法或類,每種方法只有一個簡單的目的。這樣整個程序便於閱讀,理解和維護。

請注意例如scanner對象是如何在readInput方法的本地上下文中使用的。

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class KillerLoop { 

private boolean notReady; 
private int grade; 

public static void main(String[] args) { 

    new KillerLoop(); 

} 

/** 
* the default constructor calls the doStuff method 
* which contains the main loop of the program 
*/ 
public KillerLoop() { 
    this.notReady = true; 
    doStuff(); 
} 

/** 
* the programs main loop 
*/ 
private void doStuff() { 
    while (this.notReady) { 
     int input = this.readInput(); 
     this.verifyInput(input); 
    } 
    System.out.println("Grade " + this.grade + " is a correct grade!"); 
} 

/** 
* verifies a users input 
* if the input is correct, notReady will be set 
* to false so that the programs main loop is left 
* (you could also use an if construct with break for this purpose) 
* @param userGrade the users input 
*/ 
private void verifyInput(int userGrade) { 
    if (userGrade < 9 || userGrade > 12) { 
     System.out.println("That is not a valid grade!\n" + "Grade (9-12): "); 
    } else { 
     this.grade = userGrade; 
     this.notReady = false; 
    } 

} 

/** 
* this method reads input from the command line 
* and returns an integer if successful 
* @return the users input as integer 
*/ 
private int readInput() { 
    Scanner scanner = new Scanner(System.in); 
    System.out.println("enter a grade"); 

    int userGrade = 0; 

    try { 
     userGrade = scanner.nextInt(); 
    } catch (InputMismatchException e) { 
     System.out.println("That is not a number! Enter in a valid number."); 
     this.readInput(); //this recursion might not always be a good idea ;) 
    } 
    return userGrade; 
} 
} 
相關問題