2014-03-03 30 views
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); 
} 

} 
+0

可能重复[跳跃nextLine()后使用nextInt()](http://stackoverflow.com/questions/13102045/skipping -nextline-after-use-nextint) –

回答

0

的你在各if-else statment使用break操作,因为你的循环退出后:

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

删除,并且你希望你的循环会工作。

0

问题是,无论发生什么事,你都会跳出break的循环。两种情况下都执行break指令(X == 0和X == 1)。因此,请删除此内容(如果您希望再次尝试该问题)或将其替换为继续。这样循环重新启动并且不退出。

你可以试试这个,试试这个(未测试):

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

如果你在显示代码之前解释了你提出的修改,它会更清晰。 –

相关问题