簡體   English   中英

有人可以告訴我為什么這不能打印正確的分數嗎? 它只是退出循環

[英]Can someone tell me why this is not printing the correct score; it just exits out of the loop

/*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 );
}

}

您在每個if-else語句中使用break運算符,因為循環在以下時間退出:

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

刪除它,您的循環將按預期工作。

問題是,你打破了你的循環與break無論發生什么事情。 在兩種情況下(X == 0和X == 1)都執行break指令。 因此,請將其刪除(如果您希望問題再試一次)或將其替換為繼續。 這樣,循環將重新啟動並且不會退出。

您可以嘗試一下,嘗試一下(未測試):

    /*************************************************************
     *  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!
     ************************************************************/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM