I just started to learn Java, and I'm a bit lost as to why my "break" command at the bottom of the code is being executed.
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
}
}
}
}
Considering that the keepGoing
boolean variable is still true (if the user types 'yes'), the application will still stop because of the break in the else statement. Can someone let me know why it's doing that and how to fix that?