(JAVA)Break命令在不应';T

(JAVA)Break命令在不应';T,java,if-statement,break,Java,If Statement,Break,我刚刚开始学习Java,对于为什么要执行代码底部的“break”命令,我有点不知所措 import java.util.Scanner; public class GradeValues { private static Scanner scanner; public static void main(String[] args) { boolean keepGoing = true; String grade; // initializin

我刚刚开始学习Java,对于为什么要执行代码底部的“break”命令,我有点不知所措

import java.util.Scanner;

public class GradeValues {

    private static Scanner scanner;

    public static void main(String[] args) {
        boolean keepGoing = true;
        String grade; // initializing variable for grade

        while(keepGoing = true){
            System.out.println("What percentage did you get? ");  //Ask User the question
            scanner = new Scanner(System.in); //starting the scanner 
            grade = scanner.nextLine();  //storing user input of percentage

            int percentage = Integer.parseInt(grade);  //converting string to a int

            if(percentage >= 80 && percentage <= 100){
                System.out.println("Your grade is an A!  Awesome job!");
            }else if(percentage >= 70 && percentage <= 79){
                System.out.println("Your grade is an B!  Nice work!");
            }else if(percentage >= 60 && percentage <= 69){
                System.out.println("Your grade is an C!  That's average. =( ");
            }else if(percentage >= 50 && percentage <= 59){
                System.out.println("Your grade is an D!  Come on man, you can do better.");
            }else if(percentage < 50){
                System.out.println("Your grade is an F!  You need to hit the books again and try again.");
            }else{
                System.out.println("I think you type the wrong value.");
            }

            System.out.println("Would you like to check another grade?");  //Asking user if they want to do it again
            Scanner choice = new Scanner(System.in);  //Gets user input
            String answer = choice.nextLine();  // Stores input in variable "answer"
            if(answer == "yes"){
                keepGoing = true;  //While loop should keep going

            }else{
                keepGoing = false;  //While loop should stop
                break;  //this should stop program
            }
        }
    }
}
import java.util.Scanner;
公共类价值观{
专用静态扫描仪;
公共静态void main(字符串[]args){
布尔值keepGoing=true;
字符串等级;//初始化等级的变量
while(keepGoing=true){
System.out.println(“您得到了多少百分比?”);//向用户提问
scanner=新扫描仪(System.in);//启动扫描仪
grade=scanner.nextLine();//存储用户输入的百分比
int percentage=Integer.parseInt(grade);//将字符串转换为int

如果(percentage>=80&&percentage=70&&percentage=60&&percentage=50&&percentage
answer
是一个字符串,它是一种对象类型。您应该将其与
equals
方法进行比较,而不是与
=
运算符进行比较:

if (answer.equals("yes")) {
    // etc.

不能将字符串与==运算符进行比较

为了正确比较字符串,必须使用
equals
方法

例如,将代码中的if/else语句替换为:

if("yes".equals(answer)){
    keepGoing = true;  //While loop should keep going

}else{
    keepGoing = false;  //While loop should stop
    break;  //this should stop program 
}

我想如果它不是打字错误,那么您的while条件是不正确的,while(keepGoing=true)
…应该是
while(keepGoing==true)
while(keepGoing)
。因此,您不需要在结尾处中断。就像其他答案中建议的那样,请使用equals来比较字符串。

如何在java中比较字符串?打印答案以检查其值。同时也要修复while(keepGoing=true)。这根本不正确。当您有一个常量值时,最好将此值作为参数:
“是”,等于(回答)
以避免在代码中生成随机空指针。
answer
可能为空,您将得到NPE@VoodooCoder
answer
永远不会为空。
Scanner.nextLine
如果没有下一行,则会引发异常。这是一个很好的java编程规则。如果将来以任何其他方式检索答案,则你不必对你的代码进行任何重构!@resueman,很高兴你一直记住这一点。但我没有,先做constatnt对我来说总是很有效。这解决了我的问题。非常感谢!)