/*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);
}
}
2
A
回答
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
如果你在显示代码之前解释了你提出的修改,它会更清晰。 –
相关问题
- 1. 有人可以告诉我为什么这是恶意的..?
- 2. 有人可以告诉我为什么这是Query非常慢?
- 3. 有人可以告诉我它是什么样的加密吗?
- 4. 有人可以告诉我为什么这个程序打印-56吗?
- 5. 有人可以告诉我这是什么日期格式吗?
- 6. 有人可以告诉我这是什么意思?
- 7. 可以有人告诉我什么Ruby的帖子GET的正确语法是?
- 8. 我正在查看是否有人可以告诉我这是多么安全
- 9. 任何人都可以告诉我这是什么功能吗?
- 10. 有人可以告诉我这个代码在做什么,它的输出是什么,请
- 11. 有人可以告诉我什么是grafana“系列名栏”?
- 12. 有人可以告诉我什么是在塔
- 13. 有人可以告诉我为什么这个JavaScript函数不执行?
- 14. 有人可以向我解释这是告诉我要做什么吗?
- 15. 有人可以告诉我这段代码有什么问题...?
- 16. 有人可以告诉我这幅画有什么问题吗?
- 17. 有人可以告诉我这个脚本有什么问题
- 18. 有人可以告诉我这段R代码是干什么的吗?
- 19. 有人能告诉我为什么我的分数总是计算为零?
- 20. 任何人都可以告诉我这有什么问题吗?
- 21. 我有这个代码,但我不知道为什么它不会工作有人可以告诉我吗?
- 22. 有人可以告诉我这个字符串是什么意思?
- 23. 有人可以告诉我什么是setNull和setInt在这个声明
- 24. 有人能告诉我这个代码的问题是什么?
- 25. 欧拉项目20,有人可以告诉我为什么我的输出不正确吗?
- 26. 有人可以解释为什么答案是“不正确”而不是什么?
- 27. 有人可以告诉我为什么这段代码不会运行吗?
- 28. 有人可以告诉我,为什么这个批处理脚本不工作?
- 29. 有人可以告诉我什么是io.ReadFull和bytes.Buffer.ReadFrom在golang中的行为
- 30. 有人能告诉我这是什么编码语言?
可能重复[跳跃nextLine()后使用nextInt()](http://stackoverflow.com/questions/13102045/skipping -nextline-after-use-nextint) –