2
/*The program's purpose is to test the user's ability to correctly identify the mascots of four
universities. Users will be awarded one point for each correct response, one point will be
deducted for an incorrect response, and zero points will be deducted if the user responds "don't know." 
This means that your program will need to keep score as the user responds
to the prompts from the quiz and print that score to the console at the end of the program's
execution.*/

import java.util.Scanner;

public class MascotQuiz {

    public static void main(String[] args) {

        int score = 0;

        String greeting = 
                "In this game, I ask you four questions about mascots for "
            +"US collegiate sports teams." +
            "\nYou get 1 point for each correct answer, "
            +"0 points if you type don't know, "
            +"and you lose a point for wrong answers.";
    final String schoolOptions = "University of Michigan, "
            +"University of Nebraska, "
            +"University of Oklahoma, "
            +"University of Wisconsin";
    final String mascotOptions = 
            "Badgers, Cornhuskers, Sooners, Wolverines";
    String prompt1 = 
            "\nType 1 and I'll give you the mascot and "
            +"you give give the school. \n"
            +"Type 2 and I'll give you the school and "
            +"you give me the mascot. \n"
            +"Type 3 and I'll quit.";       

    System.out.println( greeting );

    /*************************************************************
     *  Do NOT delete, move, or change the lines of code above this:
     * All of your code should appear between these comments.
     ************************************************************/
System.out.println( prompt1 );
Scanner userInput = new Scanner(System.in);
int x = userInput.nextInt();
do{
if (x==1){
    System.out.println("Answer with one of: " + schoolOptions);
    System.out.println("Badgers ? ");
    String answer1 = userInput.nextLine();
    if ("don't know".equalsIgnoreCase(answer1)){
        score = score + 0;
    }
    else if ("University of Wisconsin".equalsIgnoreCase(answer1)){
        score++;
    }
    else {
        score--; 
    }
    break;
    }
else if (x==2){
    System.out.println("Answer with one of: " + mascotOptions);
    System.out.println("University of Wisconsin ? ");
    String answer2 = userInput.nextLine();
    if ("don't know".equalsIgnoreCase(answer2)){
        score = score + 0;
    }
    else if ("Bagders".equalsIgnoreCase(answer2)){
        score = score + 1;
    }
    else{
        score = score - 1; 
    }
    break;
}
System.out.print( "\nWant to play again? (type yes or no): " );
String play = userInput.next();
if (play.equalsIgnoreCase("no")){
x = 3;
}
else{
    x = 0;
}
}
while (x!=3);
    /*************************************************************
     *  Do NOT delete, move, or change this next line of code:
     * This should be the last line of code in your program!
     ************************************************************/
    System.out.println( "\nBye. Your score is " + score );
}

}
apaul
  • 15,557
  • 8
  • 44
  • 76
Asia M.
  • 21
  • 1

2 Answers2

0

You use break operator in each if-else statment, because of your loop exited after:

if (x==1){
...
break;
}else if (x==2){
...
break;
}

Remove that, and your loop will work as you expect.

alex2410
  • 10,424
  • 3
  • 22
  • 40
0

The problem is that you break out of your loop with break no matter what happens. The break instruction is executed in both cases (X==0 and X==1). SO remove this (if you want the question to try again) or replace it with a continue. That way the loop restarts and does not quit.

You could try this, Try this (not tested):

    /*************************************************************
     *  Do NOT delete, move, or change the lines of code above this:
     * All of your code should appear between these comments.
     ************************************************************/
System.out.println( prompt1 );
Scanner userInput = new Scanner(System.in);
int x = userInput.nextInt();
do{
if (x==1){
    System.out.println("Answer with one of: " + schoolOptions);
    System.out.println("Badgers ? ");
    String answer1 = userInput.nextLine();
    if ("don't know".equalsIgnoreCase(answer1)){
        score = score + 0;
    }
    else if ("University of Wisconsin".equalsIgnoreCase(answer1)){
        score++;
    }
    else {
        score--; 
    }
    continue;                   // <---- here
    }
else if (x==2){
    System.out.println("Answer with one of: " + mascotOptions);
    System.out.println("University of Wisconsin ? ");
    String answer2 = userInput.nextLine();
    if ("don't know".equalsIgnoreCase(answer2)){
        score = score + 0;
    }
    else if ("Bagders".equalsIgnoreCase(answer2)){
        score = score + 1;
    }
    else{
        score = score - 1; 
    }
    continue;                  // <---- and here
}
    System.out.print( "\nWant to play again? (type yes or no): " );
    String play = userInput.next();
    if (play.equalsIgnoreCase("no")){
        x = 3;
    }else{
        x = 0;                        // this should be 1 or 2
    }
}
while (x!=3);
    /*************************************************************
     *  Do NOT delete, move, or change this next line of code:
     * This should be the last line of code in your program!
     ************************************************************/
exilit
  • 1,099
  • 11
  • 22