我试图让一个程序,允许用户输入5个不同的数字;但不是显示数字,我想向用户显示他已完成了多少错误/无效输入。问题是:我不完全知道如何做到这一点,但迄今为止我已经设法制定了一个计划,但不完全正确。检测无效输入的数量(使用parseInt)
import javax.swing.JOptionPane;
public class TopicThirteen {
public static void main(String[] args) {
String msg = "";
int num = 0;
int counter;
//for loop that gets the 5 numbers from the user
for(counter = 0; counter < 5; counter++) {
try {
num = Integer.parseInt(JOptionPane.showInputDialog(
"Enter number " + (counter + 1)));
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error " + e);
}
}
//sets msg to the string equivalent of input
switch(num) {
case 1:
msg = "one Invalid Inputs";
break;
case 2:
msg = "two Invalid Inputs";
break;
case 3:
msg = "three Invalid Inputs";
break;
case 4:
msg = "four Invalid Inputs";
break;
case 5:
msg = "five Invalid Inputs";
break;
}
// displays the number in words if with in range
JOptionPane.showMessageDialog(null, msg);
}
}
+1阅读http://stackoverflow.com/help/how-to-ask ...提出问题的好方法。 – StackFlowed 2014-10-02 12:43:43