我试图制作循环以验证java中的用户输入
用户必须输入介于1和12之间的整数,但如果单击取消,则JoptionPane必须关闭
int number = 0;
boolean condition= false;
while (!condition) {
try {
txt = JOptionPane.showInputDialog(null,
"Entrez num :", "Number",
JOptionPane.PLAIN_MESSAGE);
if(txt!=null &&txt!="0" )
number = Integer.parseInt(txt);
if (number <= 1 || number > 12) {
JOptionPane.showMessageDialog(null, "Integer must be between 1 and 12");
}else
condition=true;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "you have to enter an integer");
}
}
我面临的问题是,当用户点击取消时,此条件正在弹出,但我希望Joption窗格关闭并返回主菜单
JOptionPane.showMessageDialog(null, "Integer must be between 1 and 12");
当用户在框中输入0和单击取消时,如何不混淆?
很多欣赏,
低音
答案 0 :(得分:3)
答案 1 :(得分:0)
正确的写作方式:
if(txt!=null &&txt!="0" )
{
if (number <= 1 || number > 12) {
JOptionPane.showMessageDialog(null, "Integer must be between 1 and 12");
else
number = Integer.parseInt(txt);
}
else ///He hit calcel
condition= true