简体   繁体   English

文档过滤器与KeyListener对比MaskFormatter

[英]Document Filter vs. KeyListener vs. MaskFormatter

Before you jump all over me, please remember that each of you started out at some point (only saying that because I have seen the responses). 在你跳过我之前,请记住你们每个人都在某个时候开始(只是因为我看到了这些回复)。 Yes, I am learning, but need some help with the differences between DocumentFilter, KeyListener, or any other way to only allow certain data. 是的,我正在学习,但需要一些帮助来解决DocumentFilter,KeyListener或任何其他只允许某些数据的方式之间的差异。 This is for a class, but I can turn it in as it is and receive full credit. 这是一个班级,但我可以把它翻过来并获得全额学分。 I want to restrict the answer choices to only be A or B (case-insensitive). 我想将答案选择限制为仅A或B(不区分大小写)。 I have read many articles and get more confused with each one that I read. 我读了很多文章,并且对我读过的每篇文章都感到困惑。 Please help me understand. 请帮我理解。

Code below: 代码如下:

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;

        }

    }

}

Add validation to the part where you take input from the user - 将验证添加到您从用户那里获取输入的部分 -

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

Create a method isValid which will check if the input is a valid input or not, as shown below : 创建一个方法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