2014-04-07 90 views
3

在你跳過我之前,請記住,你們每個人都在某個時候開始(只是因爲我看到了回覆)。是的,我正在學習,但需要一些關於DocumentFilter,KeyListener或任何其他方式之間差異的幫助,以便只允許某些數據。這是爲一個班,但我可以把它放在原樣,並獲得完全的信譽。我想限制答案選項只能是A或B(不區分大小寫)。我讀過很多文章,並且對於我閱讀的每篇文章都感到困惑。請幫助我理解。下面文檔過濾器與KeyListener與MaskFormatter

代碼:

import java.util.Random; 
import java.io.*; 

public class HorticultureQuiz { 

    public static void main(String[] args) { 

     // Create a randomizer Object 
     Random rand = new Random(); 
     // Object used to read the input 
     BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 
     // String Array used to maintain the questions 
     String[][] questions = new String[10][2]; 

     //Questions - 0 throuugh 9 equals 10 questions 
     questions[0][0] = "Question1 \nA: True\nB: False"; 
     questions[1][0] = "Question2 \nA: True\nB: False"; 
     questions[2][0] = "Question3 \nA: True\nB: False"; 
     questions[3][0] = "Question4 \nA: True\nB: False"; 
     questions[4][0] = "Question5 \nA: True\nB: False"; 
     questions[4][0] = "Question5 \nA: True\nB: False"; 
     questions[5][0] = "Question6 \nA: True\nB: False"; 
     questions[6][0] = "Question7 \nA: True\nB: False"; 
     questions[7][0] = "Question8 \nA: True\nB: False"; 
     questions[8][0] = "Question9\nA: True\nB: False"; 
     questions[9][0] = "Question10 \nA: True\nB: False"; 

     //Answers 
     questions[0][1] = "B"; 
     questions[1][1] = "B"; 
     questions[2][1] = "B"; 
     questions[3][1] = "B"; 
     questions[4][1] = "B"; 
     questions[5][1] = "B"; 
     questions[6][1] = "B"; 
     questions[7][1] = "B"; 
     questions[8][1] = "B"; 
     questions[9][1] = "B"; 


     int intOption; 
     String strAnswer = null; 

     // Used to maintain the count in the loop 
     int ii = 0; 

     while (ii < 5){ 

     // Assign the input answer a value until you reach 5 
     intOption = rand.nextInt(5); 
     // Print the question to the screen 
     System.out.println(questions[intOption][0]); 
     //Error handling 
     try { 
      strAnswer = input.readLine(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     // If the input answer equals the correct answer in the array list 
     if (strAnswer.equals(questions[intOption][1])){ 

      // you are correct 
      System.out.println("Correct!"); 

      } 

      else{ 

      // Otherwise...WRONG 
      System.out.println("WRONG, the answer was " + questions[intOption][1]); 

      } 

     //Increment by one until you reach 5 
     ii = ii + 1; 

     } 

    } 

} 
+0

你可以這樣做的一種方法是檢查輸入是「a」,「b」,「A」還是「B」 '用同樣的方法檢查他們是否得到了正確的答案(用'String.equals'),如果不是,你可以打印一條消息並要求另一個輸入。只需重複這一過程。它可以用你用來編寫這段代碼的函數完成。 – chilemagic

+0

您的代碼不使用文檔過濾器,按鍵偵聽器或掩碼格式器。如果您的代碼顯示您已嘗試使用這些方法,您將獲得更多回復。對我而言,問題標題和代碼之間只有非常微妙的聯繫。 –

+0

再一次,我甚至都不明白它甚至可以使用它。初學者在這裏。我不知道如何使用它......這就是我問的原因。 – cokeefe28

回答

1

添加驗證到你從用戶輸入的部分 -

 System.out.println(questions[intOption][0]); 
     // Error handling 
     try { 
      do { 
       strAnswer = input.readLine().toUpperCase(); 
      } while (!isValid(strAnswer)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

創建方法isValid這將檢查輸入是一個有效的輸入或不,如下圖所示:

private static boolean isValid(String strAnswer) { 
    boolean valid = strAnswer.equals("A") || strAnswer.equals("B"); 
    if(!valid){ 
     System.out.println("\nInput is not valid, please enter a valid choice - A or B"); 
    } 
    return valid; 
} 
+0

這實際上工作得很好,但我確實有一個問題。如果這很簡單,那麼爲什麼我會在主題欄中提到的主題看到這麼多?對於它的價值,我在使用帶有MaskFormatter和NumberFormatter的JFormattedTextField創建表單時看到了這些內容。在我看來,如果有100種方法可以做某些事情,其中​​一種方法將成爲特定用例的首選。 – cokeefe28

+0

Simon對您問題的評論回答了您在此評論中提及的內容。至於我提供的簡單解決方案,那是因爲你寫了「請記住,你們每個人都在某個時候開始了」。隨着您在Java中的進步,您將瞭解到它不是爲了複雜性而使用類,而是關於上下文。 –

+0

你是非常正確的。非常感謝您抽出時間,並將其保持在我能理解的水平。 – cokeefe28