-1

I want my program to be terminated after I input "N" or "n", but when I do that, it instead keeps requesting more input. What do I need to do to fix this?

public static void main (String[] args){
    Scanner userInputReader = new Scanner(System.in);

    System.out.print("Your name: ");
    String name = userInputReader.nextLine();

    System.out.print("Gender required (M/F): ");
    char genDer = userInputReader.next().charAt(0);


    System.out.print("Your age: ");
    int age = userInputReader.nextInt();
    System.out.print("Your Resting Heart Rate: ");
    int hrRest = userInputReader.nextInt();

    double hrMax;
    double vo2Max;


    while (age > 0 && hrRest > 0 && genDer != 'M' || genDer != 'm' && genDer != 'F' || genDer != 'f') {
        while (age < 0) {
            System.out.println("Your age cannot be Negative");
            System.out.print("Please enter your age again: ");
            age = userInputReader.nextInt();
        }
        while (hrRest < 0) {
            System.out.println("Invalid resting heart rate");
            System.out.print("Please type in Your Resting Heart Rate more carefully: ");
            hrRest = userInputReader.nextInt();
        }

        while (true) {
            System.out.print("Type Y to show the results/N to stop: \n");
            char answer = userInputReader.next().charAt(0);

            if (answer == 'Y' || answer == 'y') {
                hrMax = 220 - age;
                vo2Max = 15 * (hrMax / hrRest);

                //  System.out.println("Display hrMax: " + hrMax);
                //  System.out.println("Display vo2Max: " + vo2Max);

                String typeofSports = "";
                if (genDer == 'M' || genDer == 'm') {
                    if (age >= 18 && age <= 30 && vo2Max >= 40 && vo2Max <= 60) {
                        typeofSports += "\n ---> Basketball";
                    }
                    if (age >= 18 && age <= 26 && vo2Max >= 62 && vo2Max <= 74) {
                        typeofSports += "\n ---> Cycling";
                    }
                    if (age >= 18 && age <= 26 && vo2Max >= 55 && vo2Max <= 67) {
                        typeofSports += "\n ---> Canoeing";
                    }
                    if (age >= 18 && age <= 22 && vo2Max >= 52 && vo2Max <= 58) {
                        typeofSports += "\n ---> Gymnastics";
                    }
                    if (age >= 10 && age <= 25 && vo2Max >= 50 && vo2Max <= 70) {
                        typeofSports += "\n ---> Swimming";
                    } else {
                        System.out.println("Look!!! We have calculated the final result---> \n ");
                    }
                }

                if (genDer == 'F' || genDer == 'f') {
                    if (age >= 18 && age <= 30 && vo2Max >= 43 && vo2Max <= 60) {
                        typeofSports += "\n ---> Basketball";
                    }
                    if (age >= 18 && age <= 26 && vo2Max >= 47 && vo2Max <= 57) {
                        typeofSports += "\n ---> Cycling";
                    }
                    if (age >= 18 && age <= 26 && vo2Max >= 47 && vo2Max <= 67) {
                        typeofSports += "\n ---> Canoeing";
                    }
                    if (age >= 18 && age <= 22 && vo2Max >= 36 && vo2Max <= 50) {
                        typeofSports += "\n ---> Gymnastics";
                    }
                    if (age >= 10 && age <= 25 && vo2Max >= 40 && vo2Max <= 60) {
                        typeofSports += "\n ---> Swimming";
                    } else {
                        System.out.println("Look!!! We have calculated the final result---> \n ");
                    }
                }

                System.out.println("Recommended sports are: " + typeofSports);

                // update the condition
                System.out.print("");
                name = userInputReader.nextLine();

                System.out.print("Gender required (M/F): ");
                genDer = userInputReader.next().charAt(0);


                System.out.print("Your age: ");
                age = userInputReader.nextInt();
                System.out.print("Your Resting Heart Rate: ");
                hrRest = userInputReader.nextInt();
            }
            if (answer == 'N' || answer == 'n') {
                break; // What should I put here?
            }
        }
Tom
  • 16,842
  • 17
  • 45
  • 54
  • 2
    This should definitely be considered: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – maloomeister Jun 17 '21 at 09:59
  • Yes, thank you so much. I'm reading it right now. – BlueWhaleHat Jun 17 '21 at 10:01

1 Answers1

0

Your break in:

while (age > 0 && hrRest > 0 && genDer != 'M' || genDer != 'm' && genDer != 'F' || genDer != 'f') {
    ...
    while(true) {
        ...
        if (answer == 'N' || answer == 'n') {
            break; // What should i put here?
        }
        ...
    }
    ...
}

Only breaks the nested while loop while(true) {...}, but not the outer loop. You can either use return instead of break, or add a flag boolean stop = false; to in the outer loop and set to true when input is 'N' || 'n', then add a checking in the outer loop to break again when stop == true.

Alphaharrius
  • 210
  • 1
  • 9