Java while循环测试不';t重置
作为我课程工作的一部分,我的任务是创建一个简单的问答游戏,循环直到用户选择正确的答案。 当答案正确(答案为B)或不正确时,我可以生成消息提示和响应提示,但我很难让while循环复位并再次提示用户。 目前,当用户得到错误的答案时,它会在响应提示上无限循环Java while循环测试不';t重置,java,string,while-loop,Java,String,While Loop,作为我课程工作的一部分,我的任务是创建一个简单的问答游戏,循环直到用户选择正确的答案。 当答案正确(答案为B)或不正确时,我可以生成消息提示和响应提示,但我很难让while循环复位并再次提示用户。 目前,当用户得到错误的答案时,它会在响应提示上无限循环 import javax.swing.JOptionPane; public class Quiz { public static void main(String[] args) { String question =
import javax.swing.JOptionPane;
public class Quiz {
public static void main(String[] args) {
String question = "What colour is the sky?\n";
question += "A. Purple\n";
question += "B. Blue\n";
question += "C. Green\n";
question += "D. Yellow\n";
question += "E. Orange\n";
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
int guess = 0;
while (answer != "B")
if (answer.equals("B")) {
JOptionPane.showMessageDialog(null,"Correct!");
break;
}
else if (answer.equals("A")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("C")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("D")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("E")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else {
JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
}
guess++;
}
}
而不是
while (answer != "B"){
answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
if (answer.equals("B")) {
JOptionPane.showMessageDialog(null,"Correct!");
break;
}
else if (answer.equals("A")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("C")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("D")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("E")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else {
JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
}
guess++;
}
你错过了大括号。而不是
while (answer != "B"){
answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
if (answer.equals("B")) {
JOptionPane.showMessageDialog(null,"Correct!");
break;
}
else if (answer.equals("A")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("C")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("D")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else if (answer.equals("E")) {
JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
}
else {
JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
}
guess++;
}
你错过了大括号。你忘了再给一次回答问题的机会。 现在它正按照你的预期工作
public static void main(String[] args) {
String question = "What is the Capital of South Africa?\n";
question += "A. Cape town\n";
question += "B. Pretoria\n";
question += "C. Johannesburg\n";
question += "D. Durban\n";
question += "E. Nelspruit\n";
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
int guess = 0;
while (answer != "B") {
answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
if (answer.equals("B")) {
JOptionPane.showMessageDialog(null, "Correct!");
break;
} else if (answer.equals("A")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
} else if (answer.equals("C")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
} else if (answer.equals("D")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
} else if (answer.equals("E")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
} else {
JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
}
guess++;
}
}
你忘了再给一次机会回答这个问题。 现在它正按照你的预期工作
public static void main(String[] args) {
String question = "What is the Capital of South Africa?\n";
question += "A. Cape town\n";
question += "B. Pretoria\n";
question += "C. Johannesburg\n";
question += "D. Durban\n";
question += "E. Nelspruit\n";
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
int guess = 0;
while (answer != "B") {
answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
if (answer.equals("B")) {
JOptionPane.showMessageDialog(null, "Correct!");
break;
} else if (answer.equals("A")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
} else if (answer.equals("C")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
} else if (answer.equals("D")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
} else if (answer.equals("E")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
} else {
JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
}
guess++;
}
}
如果用户输入不是
B
,则必须反复提示问题。在while循环中,您需要更改变量answer
的值。尝试下面的工作片段
public class Quiz{
public static void main(String[] args) throws IOException {
String answer = promptQuestion();
while (answer != "B") {
if (answer.equals("B")) {
JOptionPane.showMessageDialog(null, "Correct!");
break;
} else if (answer.equals("A")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
answer = promptQuestion();
} else if (answer.equals("C")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
answer = promptQuestion();
} else if (answer.equals("D")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
answer = promptQuestion();
} else if (answer.equals("E")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
answer = promptQuestion();
} else {
JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
answer = promptQuestion();
}
}
}
public static String promptQuestion() {
String question = "What is the Capital of South Africa?\n";
question += "A. Cape town\n";
question += "B. Pretoria\n";
question += "C. Johannesburg\n";
question += "D. Durban\n";
question += "E. Nelspruit\n";
String answer = JOptionPane.showInputDialog(question);
return answer.toUpperCase();
}
}
如果用户输入不是
B
,则必须反复提示问题。在while循环中,您需要更改变量answer
的值。尝试下面的工作片段
public class Quiz{
public static void main(String[] args) throws IOException {
String answer = promptQuestion();
while (answer != "B") {
if (answer.equals("B")) {
JOptionPane.showMessageDialog(null, "Correct!");
break;
} else if (answer.equals("A")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
answer = promptQuestion();
} else if (answer.equals("C")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
answer = promptQuestion();
} else if (answer.equals("D")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
answer = promptQuestion();
} else if (answer.equals("E")) {
JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
answer = promptQuestion();
} else {
JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
answer = promptQuestion();
}
}
}
public static String promptQuestion() {
String question = "What is the Capital of South Africa?\n";
question += "A. Cape town\n";
question += "B. Pretoria\n";
question += "C. Johannesburg\n";
question += "D. Durban\n";
question += "E. Nelspruit\n";
String answer = JOptionPane.showInputDialog(question);
return answer.toUpperCase();
}
}
guess++代码>不是while
循环的一部分,仅供参考。还有while(answer!=“B”)
和if(answer.equals(“B”)
这些语句是相互矛盾的。在while循环中,使用括号({…}
)作为循环。在while循环中,您永远不会更改变量answer
的值。因此循环要么根本不运行,要么无限次运行。如果用equals
检查相等,为什么用检查不相等=代码>?看看guess++代码>不是while
循环的一部分,仅供参考。还有while(answer!=“B”)
和if(answer.equals(“B”)
这些语句是相互矛盾的。在while循环中,使用括号({…}
)作为循环。在while循环中,您永远不会更改变量answer
的值。因此循环要么根本不运行,要么无限次运行。如果用equals
检查相等,为什么用检查不相等=代码>?看一看,而不是什么?另外,如果你指的是大括号,那不是唯一的问题。while(答案!=“B”)
应该是while(“B”。equals(答案))
@karthikeyanvaitillingam哦,我想你是对的!字符串值必须使用等于函数。而不是什么?另外,如果你指的是大括号,那不是唯一的问题。while(答案!=“B”)
应该是while(“B”。equals(答案))
@karthikeyanvaitillingam哦,我想你是对的!字符串值必须使用equals函数。一些建议将answer=JOptionPane.showInputDialog(问题);answer=answer.toUpperCase()代码>在while循环的末尾也while(答案!=“B”)
应该是while(“B”.equals(答案))
一些建议将answer=JOptionPane.showInputDialog(问题);answer=answer.toUpperCase()也while(答案!=“B”)
应该是while(“B”。等于(答案))