简体   繁体   English

使用while循环验证布尔输入

[英]Validating boolean input using while loop

I'm doing some Java homework and I can't get this while loop to work correctly. 我正在做一些Java作业,而while循环无法正常工作。

I've tried to editing the code, but it keeps giving errors. 我试图编辑代码,但是它总是出错。

while(true) {
            System.out.println("Is student a TA? (1: YES 0: NO): ");
                status = inReader.nextInt();
                    if (status == 1) {
                        status1 = true;
                    }
                    else if (status == 0) {
                            status1 = false;
                    }
                    else {
                        System.out.println("Please enter valid entry.");
                    } 
            Graduate grad = new Graduate(studentID, name, major, status1);
            grad.displayStudentData();
                }   
            }
        }
   }

I want the code to loop back if the user inputs anything other than a 0 or 1. It does loop back but the information gets outputted to the console. 我希望代码在用户输入非0或1的情况下环回。它确实会环回,但信息会输出到控制台。 Which is not what I was hoping it would do. 这不是我希望的。

I'm going to assume that what you are doing with the grad variable is the information you don't want displayed when you get an invalid answer. 我将假设您使用grad变量所做的就是您得到无效答案时不希望显示的信息。 I'm also assuming you eventually want to exit the loop. 我还假设您最终要退出循环。

boolean doLoop = true;
Graduate grad = null;

while(doLoop) {
        System.out.println("Is student a TA? (1: YES 0: NO): ");
        status = inReader.nextInt();

        if (status == 1) {
            grad = new Graduate(studentID, name, major, true);
            doLoop  = false;
        }
        else if (status == 0) {
            grad = new Graduate(studentID, name, major, false);
            doLoop  = false;
        }
        else {
            System.out.println("Please enter valid entry.");
        } 

        if (grad != null){
            grad.displayStudentData();
        }
    }
}

Checking for a null allows you to only display something when there's something to display. 如果检查为null ,则仅在有显示内容时才显示某些内容。

Getting rid of the status1 variable reduces the number of variables you need to mentally keep track of. 摆脱status1变量可以减少需要status1变量数量。 That variable isn't necessary anyway, since there are better ways to deal with validating an input. 无论如何,该变量不是必需的,因为有更好的方法来验证输入。

Setting the doLoop variable allows you to exit the while when you need to. 设置doLoop变量可以让你退出while当你需要。

Also, the code you posted had a few extra close brackets. 另外,您发布的代码还有一些额外的括号。 It's a good idea to make sure the code you post is either runnable or pseudo-code. 确保发布的代码是可运行的或伪代码是一个好主意。

I suggest that you take a step back from writing code and describe the steps in English (or any other written human language). 我建议您从编写代码后退一步,并以英语(或任何其他书面人类语言)描述这些步骤。 For example, you can write something like this: 例如,您可以编写如下内容:

while input is invalid
    get input
    check if input is valid
create a Graduate object
display data

This is called "pseudocode". 这称为“伪代码”。 Note how I use indentation similar to how we write code to indicate what steps are repeated in the while loop. 注意,我如何使用缩进方式,类似于我们编写代码的方式,以指示在while循环中重复哪些步骤。 This also shows how the last two steps should not be inside the while loop. 这也显示了最后两个步骤应该在while循环内。

If you want to be able to do this for multiple students then there should be another loop around this one. 如果你希望能够为多个学生这样做,那么应该围绕这一另一个循环。 Again, write the steps in words to figure out the exact structure that you need. 同样,用文字写出步骤以找出所需的确切结构。

Solution : 解决方案

boolean run = true;
while(run){

    boolean valid = true;
    System.out.println("Is student a TA? (1: YES 0: NO): ");
    status = inReader.nextInt();
    boolean status;
    if (status == 1) {
        status1 = true;
        run = false;
    }
    else if (status == 0) {
        status1 = false;
        run = false;
    }
    else {
        System.out.println("Please enter valid entry.");
        valid = false;
    } 
    if(valid){ //or if(valid == true)
        Graduate grad = new Graduate(studentID, name, major, status1);
        grad.displayStudentData();
    }
}  

What you have going on is that once input that is not 1 or 0 is inputted, you will execute the code in the else block. 您正在执行的操作是,一旦输入了非10的输入,就将在else块中执行代码。 Then your code will execute 然后您的代码将执行

Graduate grad = new Graduate(studentID, name, major, status1);
grad.displayStudentData();

If you didn't want that to be executed, add continue; 如果您不希望执行该操作,请添加continue; to the end of the else block. else块的末尾。

You say it yourself, 你自己说

I want the code to loop back if the user inputs anything other than a 0 or 1 如果用户输入的不是0或1,我希望代码环回

so why not put that in the condition? 那为什么不把它放在条件上呢?

// initialize with invalid value so we enter the loop at least once
int input = -1;
while (input != 0 && input != 1) {
    System.out.println("Is student a TA? (1: YES 0: NO): ");
    input = inReader.nextInt();
}
// now you're guaranteed to have a 0 or 1 in the input
boolean status = input == 1; // will the true on 1, false on 0
Graduate grad = new Graduate(studentID, name, major, status);
grad.displayStudentData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM