2

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; 


        }

    }


}
2
  • You didn't update choice inside the loop. Commented Feb 17, 2014 at 15:35
  • It is good style to leave a space after if, while, for and switch, but not inside the brackets (e.g. do if (flag) but not if ( flag ). Commented Feb 17, 2014 at 15:39

2 Answers 2

4

You should assign to choice inside the loop, not outside it. Your current code sets it once and never changes it again.

Sign up to request clarification or add additional context in comments.

4 Comments

Alright, I moved it inside the loop (I edited the code) but I'm still getting the same issue :/
Now it isn't looping, but when I enter a number, and press enter it just puts a new space below the number that I've entered. It isn't saving the input and bringing the user back to the 4 choice menu.
@user3304333: You are still printing the menu outside the loop.
I have everything working smoothly, but I would like to know how I can enter more than 1 grade in Case 2 without going back to the main menu or having the user have to input a number to exit the loop. I want them to be able to enter how ever many numbers they would like, then press enter to average all of those grades.
0

This --> choice = input.nextInt(); Should be inside of --> while ( choice != 4 ) at the very end.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.