我試圖閱讀有關使用while循環預測試條件的一個小程序,它將編譯響應和輸出數據,但我有一個問題,無論我輸入什麼輸入框告訴我它是無效的。我不確定有什麼問題。這是相關的代碼。如果語句內while循環錯誤
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);
}
}
}
}
您是否嘗試過調試它?調試是關鍵,它有助於95%的案例。在每行代碼中,inputString的值是什麼?另外,請注意,除了3 **以外,您的'else'語句將觸發**任何輸入值。 –