Taylor Taylor - 7 months ago 9
Java Question

Why won't this loop return to the beginning after user has input y to continue?

Not quite sure what is wrong with this. I've tried a do-while and a switch as well, but nothing seems to be working. Is there something I'm missing? When the code executes and the user enters Y to continue, it just ends, but I need it to go back to the beginning so that the user can input another grade.

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your grade: ");
double grade = input.nextDouble();
while (grade >=0 &&grade <=100)
if(grade<=100&&grade>=90)
{
System.out.println("Your grade is an A"); break;
} else if (grade <=89&&grade>=80){
System.out.println("Your grade is a B"); break;
} else if (grade <=79&&grade>=70) {
System.out.println("Your grade is a C"); break;
} else if (grade <=69&&grade>=60) {
System.out.println("Your grade is a D"); break;
} else if (grade <=59) {
System.out.println("Your grade is an F");break;
}
System.out.println("Do you want to continue (Y/N)?");
char check = input.next().charAt(0);
}

}

Answer

The problem with your current implementation is that first of all, you don't let the user insert more than one grade, by breaking each if statement.

Back to the initial words, the user input, regarding resuming with grades' insertion isn't handled inside a loop. A do-while loop should be used, instead.

public static void main(String[] args) {

 boolean flag = false;
 Scanner input = new Scanner(System.in);


 double grade;
 do {
  System.out.println("Enter your grade: ");
  grade = input.nextDouble();
  if (grade <= 100 && grade >= 90) {
   System.out.println("Your grade is an A");
  } else if (grade <= 89 && grade >= 80) {
   System.out.println("Your grade is a B");
  } else if (grade <= 79 && grade >= 70) {
   System.out.println("Your grade is a C");
  } else if (grade <= 69 && grade >= 60) {
   System.out.println("Your grade is a D");
  } else if (grade <= 59) {
   System.out.println("Your grade is an F");
  }

  System.out.println("Do you want to continue (Y/N)?");
  char check = input.next().charAt(0);
  if (check == 'Y') {flag = true;}
  else {flag = false;}
 } while (grade >= 0 && grade <= 100 && flag == true);
}

Generally, the above (breaking if-else statements) is considered a bad practice and I foresee that you confused the usage of switch statement with the if-else.

On a switch statement, break is used as we only want one of the examined conditions to be satisfied. In case of break absence, once a match is made to a case, execution will continue to the rest of the remaining cases that are written below the matched case.

On the other hand, the if-else code will only enter the statement that matches and will not execute any other if-else statement, until the end of the last if-else (or else) statement, so, there's no need to use break here, as the compiler itself knows that if it makes a match on an if statement, it will then move after the end of that block, after having executing the matched if (or if-else or else) statement.

And this is for you further reference, regarding my fore-mentioned words.

Comments