简体   繁体   English

字符串输入匹配条件后,while循环不断重复

[英]Do while loop keeps repeating after string input matches condition

The code below is what I have written so far, so nothing has been implemented.下面的代码是我到目前为止所写的,所以什么都没有实现。 However in the program it asks the user if the lowest grade is dropped.然而,在程序中,它会询问用户是否放弃最低等级。 If the user replies by saying either "Yes","yes","No",or"no" the do loop should end and keep going through the rest of the code.如果用户回答说“是”、“是”、“否”或“否”,那么 do 循环应该结束并继续执行其余的代码。

However even if i input those correct responses it keeps going through the loop.但是,即使我输入了那些正确的响应,它也会继续循环。 I'm pretty new a java, so apologies if this is really stupid.我是一个 Java 新手,如果这真的很愚蠢,我很抱歉。

Code:代码:

import java.util.Scanner;
public class AvgGrade {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int numTest=1;


    //Test Number Checker
    while(numTest<=1) {
        System.out.println("How many tests were given out");
        System.out.println("Must be more than 1");
        numTest=input.nextInt();
    }

    //Lowest Grade Dropped Checker
    boolean droppedLow=false;
    String dropAnswer="default";
    do{
        System.out.println("Is the lowest grade dropped?");
        System.out.println("Answer: Yes, yes, No, no");
        dropAnswer=input.next();
    }
    while(dropAnswer.compareTo("Yes")!=0||dropAnswer.compareTo("yes")!=0||
              dropAnswer.compareTo("No")!=0||dropAnswer.compareTo("no")!=0);

    if(dropAnswer.equals("Yes")||dropAnswer.equals("yes"))
        droppedLow=true;
    else if(dropAnswer.equals("No")||dropAnswer.equals("no"))
        droppedLow=false;




    System.out.println("----------------DEBUG----------------");
    System.out.println(numTest);
    System.out.println(dropAnswer);
    System.out.println(droppedLow);
}

} }

Console/Output:控制台/输出:

How many tests were given out
Must be more than 1
5
Is the lowest grade dropped?
Answer: Yes, yes, No, no
Yes
Is the lowest grade dropped?
Answer: Yes, yes, No, no
yes
Is the lowest grade dropped?
Answer: Yes, yes, No, no
no
Is the lowest grade dropped?
Answer: Yes, yes, No, no
No
Is the lowest grade dropped?
Answer: Yes, yes, No, no

This will always be true no matter what you type, so the loop will continue forever:无论您输入什么,这始终是正确的,因此循环将永远继续:

dropAnswer.compareTo("Yes")!=0||dropAnswer.compareTo("yes")!=0||
    dropAnswer.compareTo("No")!=0||dropAnswer.compareTo("no")!=0

Essentially, your condition means "loop if dropAnswer is unequal to 'Yes' OR it's unequal to 'yes' OR it's unequal to 'No' OR it's unequal to 'no.'" At least three of those conditions are always going to be true, since there's no string that's 'Yes', 'yes', 'No', and 'no' simultaneously.本质上,您的条件意味着“如果 dropAnswer 不等于 'Yes'它不等于 'yes'它不等于 'No'它不等于 'no',则循环。” 这些条件中至少有三个总是为真,因为没有同时为“是”、“是”、“否”和“否”的字符串。

I think you meant to use && (and) instead of ||我认为您打算使用&& (and) 而不是|| (or): (或者):

dropAnswer.compareTo("Yes") != 0 && dropAnswer.compareTo("yes") != 0 &&
    dropAnswer.compareTo("No") != 0 && dropAnswer.compareTo("no") != 0

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

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