Java while循环错误中的If语句
我试图阅读关于使用while循环作为一个小程序的预测试条件,该程序将编译响应并输出它们的数据,但我遇到了一个问题,即无论我在输入框中输入什么,它都会告诉我它是无效的。我不知道怎么了。这是相关代码Java while循环错误中的If语句,java,Java,我试图阅读关于使用while循环作为一个小程序的预测试条件,该程序将编译响应并输出它们的数据,但我遇到了一个问题,即无论我在输入框中输入什么,它都会告诉我它是无效的。我不知道怎么了。这是相关代码 import javax.swing.JOptionPane; public class SurveySummarization { public static void main(String[] args) { int agree = 0; in
import javax.swing.JOptionPane;
public class SurveySummarization
{
public static void main(String[] args)
{
int agree = 0;
int disagree = 0;
int neutral = 0;
int totalVotes = 0;
int input;
String inputString;
inputString = JOptionPane.showInputDialog("Response: \n" +
"(1=agree, 2=disagree, 3=no opinion, -1=exit)");
input = Integer.parseInt(inputString);
while (input != -1)
{
if (input == 1)
{
agree += 1;
totalVotes += 1;
}
if (input == 2)
{
disagree += 1;
totalVotes += 1;
}
if (input == 3)
{
neutral += 1;
totalVotes += 1;
}
else {
JOptionPane.showMessageDialog(null, "invalid response "
+ input);
}
}
}
}
这是因为你没有正确地使用
或者。如果你看你的代码,你最后的If
是
if (input == 3)
{
neutral += 1;
totalVotes += 1;
}
else {
JOptionPane.showMessageDialog(null, "invalid response "
+ input);
}
意思是如果输入!=3、显示无效响应
要解决此问题,请将if更改为else if(输入==2)
。。。(对于==3也是一样)。这是因为你没有正确地使用或者。如果你看你的代码,你最后的If
是
if (input == 3)
{
neutral += 1;
totalVotes += 1;
}
else {
JOptionPane.showMessageDialog(null, "invalid response "
+ input);
}
意思是如果输入!=3、显示无效响应
要解决此问题,请将if更改为else if(输入==2)
。。。(与==3相同)。正如Steve所指出的,if的没有正确放置。我想你是想把else if,而不是单独的if
import javax.swing.JOptionPane;
public class SurveySummarization
{
public static void main(String[] args)
{
int agree = 0;
int disagree = 0;
int neutral = 0;
int totalVotes = 0;
int input;
String inputString;
inputString = JOptionPane.showInputDialog("Response: \n" +
"(1=agree, 2=disagree, 3=no opinion, -1=exit)");
input = Integer.parseInt(inputString);
while (input != -1)
{
if (input == 1)
{
agree += 1;
totalVotes += 1;
}else if (input == 2)
{
disagree += 1;
totalVotes += 1;
} else if (input == 3)
{
neutral += 1;
totalVotes += 1;
}
else {
JOptionPane.showMessageDialog(null, "invalid response "
+ input);
}
}
}
}
正如Steve所指出的,if没有正确放置。我想你是想把else if,而不是单独的if
import javax.swing.JOptionPane;
public class SurveySummarization
{
public static void main(String[] args)
{
int agree = 0;
int disagree = 0;
int neutral = 0;
int totalVotes = 0;
int input;
String inputString;
inputString = JOptionPane.showInputDialog("Response: \n" +
"(1=agree, 2=disagree, 3=no opinion, -1=exit)");
input = Integer.parseInt(inputString);
while (input != -1)
{
if (input == 1)
{
agree += 1;
totalVotes += 1;
}else if (input == 2)
{
disagree += 1;
totalVotes += 1;
} else if (input == 3)
{
neutral += 1;
totalVotes += 1;
}
else {
JOptionPane.showMessageDialog(null, "invalid response "
+ input);
}
}
}
}
因为您知道输入不能同时等于1、2和3,所以应该使用else if,以及第一个if和最后一个else。您当前的代码将检查输入是否等于1,是否很好。然后检查它是否等于2,但前面的语句得出的结论是输入等于1,因此不需要检查==2或==3。当链接在一起时,使用if/else if/else链接在一起只能满足一个条件。一旦达到满足条件的条件,则跳过其余条件
if (input == 1)
{
agree += 1;
totalVotes += 1;
}
else if (input == 2)
{
disagree += 1;
totalVotes += 1;
}
else if (input == 3)
{
neutral += 1;
totalVotes += 1;
}
else {
JOptionPane.showMessageDialog(null, "invalid response " + input);
}
因为您知道输入不能同时等于1、2和3,所以应该使用else if,以及第一个if和最后一个else。您当前的代码将检查输入是否等于1,是否很好。然后检查它是否等于2,但前面的语句得出的结论是输入等于1,因此不需要检查==2或==3。当链接在一起时,使用if/else if/else链接在一起只能满足一个条件。一旦达到满足条件的条件,则跳过其余条件
if (input == 1)
{
agree += 1;
totalVotes += 1;
}
else if (input == 2)
{
disagree += 1;
totalVotes += 1;
}
else if (input == 3)
{
neutral += 1;
totalVotes += 1;
}
else {
JOptionPane.showMessageDialog(null, "invalid response " + input);
}
你试过调试它吗?调试是关键,它在95%的情况下都有帮助。每行代码的inputString
,input
值是多少?另外,请注意,您的else
语句将触发除3之外的input
的任何值。您是否尝试过调试它?调试是关键,它在95%的情况下都有帮助。每行代码的inputString
,input
值是多少?另外,请注意,您的else
语句将触发除3之外的input
的任何值。好的,因此基本上else只应用于最终的if语句?没错。目前,所有的if都是完全独立的条件,计算机将逐个进行检查。您建议我使用什么方法来完成此操作?正如我在回答中所说,“要解决此问题,请将if更改为else if(输入==2)…。(“==3”)也是如此。”@Josh别忘了检查这个答案,如果这有助于解决你的问题OK,那么基本上else只适用于最后的if语句?没错。目前,所有的if都是完全独立的条件,计算机将逐个进行检查。您建议我使用什么方法来完成此操作?正如我在回答中所说,“要解决此问题,请将if更改为else if(输入==2)…。(“==3”)也是如此。”@Josh如果这有助于解决你的问题,别忘了检查这个答案