我有一个无限的while循环问题,无论我输入什么,即使它是“Filed”或“Incomplete”,循环仍然会重新提示,我无法找出原因。
strMajorSheet = JOptionPane.showInputDialog(null,"What is the advisee's major sheet status? (Filed/Incomplete)",
"Advisee's Major Sheet",3);
if(strMajorSheet == "Filed" || strMajorSheet == "Incomplete")
{
switch(strMajorSheet)
{
case "Filed":
blnMajorSheet = true;
case "Incomplete":
blnMajorSheet = false;
}
}
else
{
while(strMajorSheet != "Filed" && strMajorSheet != "Incomplete")
{
strMajorSheet = JOptionPane.showInputDialog(null,"What is the advisee's major sheet status? (Filed/Incomplete)",
"Advisee's Major Sheet",3);
}
答案 0 :(得分:0)
字符串比较Java必须使用equals
和equalsIgnoreCase
答案 1 :(得分:0)
请不要使用==
将其更改为
if(strMajorSheet.equals("Filed") || strMajorSheet.equals("Incomplete"))
......
while(!strMajorSheet.equals("Filed") && !strMajorSheet.equals("Incomplete"))
......
答案 2 :(得分:0)
首先,使用String
进行==
比较,true
比较对象引用,它们不太可能经常equals
。相反,您需要使用equalsIgnoreCase
或String
来比较两个if
的内容。
话虽如此,我不知道为什么你会对switch
语句感到困扰,因为整个逻辑只能使用boolean gotInput = true;
do {
String strMajorSheet = JOptionPane.showInputDialog(null, "What is the advisee's major sheet status? (Filed/Incomplete)",
"Advisee's Major Sheet", 3);
gotInput = true;
switch (strMajorSheet) {
case "Filed":
blnMajorSheet = true;
break;
case "Incomplete":
blnMajorSheet = false;
break;
default:
gotInput = false;
}
} while (!gotInput);
语句来实现,例如...
switch (String)
现在,请注意,String#equals
与使用{{1}}相同,也就是说,它是区分大小写的比较。