While loops on Java

Viewed 31

Not sure what I am doing wrong here or not understanding and I believe I am thrown off as far as the yes and no answer to my program I get the success the first time but when it calculates the answer it runs over itself and loops infinite

import java.util.Scanner;

public class patrickmahoney_assignment3 {

    public static void main(String[] args) {

        Scanner scan1 = new Scanner(System.in); //Setup Scanner

        System.out.println("This program will calculate a students grade for the marking period.");
        System.out.println(" ");
        System.out.println("Do you want to calculate a student's overall grade? yes/no ");

        String response = scan1.nextLine();

        do {
            System.out.println("Great! Let's get started.");
            System.out.println(" ");
            System.out.println("Please enter student's name: ");
            // user inputs students name
            String student = scan1.nextLine();

            System.out.println("Please enter grades separated by a space... A1 A2 Ex P: ");

            //Assignment 1 input by user
            int A1 = scan1.nextInt();
            //Assignment 2 input by user
            int A2 = scan1.nextInt();
            //Exercise input by user
            int Ex = scan1.nextInt();
            //Participation input by user
            int P = scan1.nextInt();

            //Print out user entered information and overall grade calculation
            System.out.println("Student: " + student);
            System.out.println("Grades: " + "\nA1= " + A1 + " " + "\nA2= " + A2 + " " + "\nEx= " + Ex + " " + "\nP= " + P);
            System.out.println("Overall grade: " + (A1 * 0.25 + A2 * 0.25 + Ex * 0.4 + P * 0.1));

        } while ("Yes".equalsIgnoreCase(response));

        do {
            System.out.println("Thanks for using the grade calculation program. ");

        } while ("No".equalsIgnoreCase(response));

        scan1.close();
    }
}

1 Answers

If you want to exit the loop then you need to change the value of response which is used in the while condition while ("Yes".equalsIgnoreCase(response)), for example System.out.println("Type exit to finish or yes to continue"); followed by response = scan1.nextLine();

For example:

    System.out.println("This program will calculate a students grade for the marking period.");
    System.out.println(" ");

    //Set this to yes so the code enters the while loop the first time
    String response = "yes";

    do {
        //Get the input to see if the user wants to continue
        System.out.println("Do you want to calculate a student's overall grade? yes/no ");
        response = scan1.nextLine();
        //Check if the response was "yes"
        if(response.equalsIgnoreCase("yes")){
            //Your code here .......
            //Removed for clarity
        }
        //If the response was anything other than "yes" then it will exit the while loop on the next cycle

    } while ("Yes".equalsIgnoreCase(response));

    System.out.println("Finished, the while loop was exited");
Related