I have a program that will allow a user to select 1 of 4 choices, then have the program execute that choice and revert back to the menu of 4 choices. For some reason, it infinitely loops the choice they choose. If I enter 1, it keeps asking for the percentage and doesn't go back to the menu, and I really don't know how I can fix this. I've looked at a few infinite loop posts but still having problems. Here is the code.
import java.util.Scanner;
public class ExerciseThree
{
public static void main ( String[] argsv )
{
float percent = 0;
double grade = 0;
double totalAvg = 0;
double total = 0;
double gradeAvg = 0;
int gradeCounter = 0;
int quit;
int choice = 0;
Scanner input = new Scanner (System.in);
System.out.println( "Please choose one of the following: \n 1 - Set percentage of total for new grades \n 2 - Enter new grades \n 3 - Get average \n 4 - Quit ");
while (choice != 4) {
choice = input.nextInt();
switch (choice)
{
case 1:
System.out.println( "Enter a percentage to multiply by" );
percent = input.nextFloat();
break;
case 2:
System.out.println( "Enter grades" );
grade = input.nextDouble();
total = total + grade;
gradeCounter = gradeCounter + 1;
gradeAvg = (double) total / gradeCounter;
break;
case 3:
System.out.println( "You have chosen to get the average" );
totalAvg = totalAvg + percent * grade;
totalAvg = input.nextDouble();
break;
default:
System.out.println( "You have chosen to quit" );
quit = input.nextInt();
break;
}
}
}
choiceinside the loop.if,while,forandswitch, but not inside the brackets (e.g. doif (flag)but notif ( flag ).