我不知道如何插入一行代码来计算弹出框中输入的数字。基本上我不能输入超过5个数字。所以我想需要输入一些if语句,我不知道该怎么做。
下面是我的代码:
String number;
number = JOptionPane.showInputDialog("Enter Number");
JOptionPane.showMessageDialog(null,"The new result is" + number,"Results",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
谢谢
发布于 2011-03-02 13:00:42
编辑:有点破旧,但是这样怎么样?
while(true)
{
String number = JOptionPane.showInputDialog("Enter Number");
if(number.length() >5 )
{
JOptionPane.showMessageDialog(null ,"Too Long! try again",JOptionPane.PLAIN_MESSAGE);
}
else break;
}
发布于 2011-03-02 13:35:26
这有一些复杂性,例如,您不检查非数字字符。
String number = JOptionPane.showInputDialog("Enter number");
number = number.trim(); // remove any spaces before and after
if (number.length() > 5 || hasNonNumeric(String)) {
// show message
JOptionPane.showMessageDialog(null,"Too long or non numeric characters in the string",
JOptionPane.PLAIN_MESSAGE);
}
boolean hasNonNumeric(String pSrc) {
for (char c : pSrc.toCharArray()) {
if (!Character.isDigit(c)) {
return true;
}
}
return false;
}
这样会更安全一些。
https://stackoverflow.com/questions/5167795
复制