簡體   English   中英

文檔過濾器與KeyListener對比MaskFormatter

[英]Document Filter vs. KeyListener vs. MaskFormatter

在你跳過我之前,請記住你們每個人都在某個時候開始(只是因為我看到了這些回復)。 是的,我正在學習,但需要一些幫助來解決DocumentFilter,KeyListener或任何其他只允許某些數據的方式之間的差異。 這是一個班級,但我可以把它翻過來並獲得全額學分。 我想將答案選擇限制為僅A或B(不區分大小寫)。 我讀了很多文章,並且對我讀過的每篇文章都感到困惑。 請幫我理解。

代碼如下:

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;

        }

    }

}

將驗證添加到您從用戶那里獲取輸入的部分 -

        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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM