Java 如果用户未输入任何内容,如何验证整数字段? do{ 试一试{ 输入=(字符串)JOptionPane.showInputDialog(null,“食物数量”,“毒品巷”,JOptionPane.QUESTION\u消息); 数量=整数.parseInt(输入); 若有(数量100){ JOptionPane.showMessageDialog(null,“无效输入。仅限数字。\n\n最小顺序=1\n最大顺序=100”,“摄影巷”,JOptionPane.ERROR\u消息); } }捕获(数字格式){ JOptionPane.showMessageDialog(null,“无效输入。仅限数字。\n\n最小顺序=1\n最大顺序=100”,“摄影巷”,JOptionPane.ERROR\u消息); } }而(数量100),;

Java 如果用户未输入任何内容,如何验证整数字段? do{ 试一试{ 输入=(字符串)JOptionPane.showInputDialog(null,“食物数量”,“毒品巷”,JOptionPane.QUESTION\u消息); 数量=整数.parseInt(输入); 若有(数量100){ JOptionPane.showMessageDialog(null,“无效输入。仅限数字。\n\n最小顺序=1\n最大顺序=100”,“摄影巷”,JOptionPane.ERROR\u消息); } }捕获(数字格式){ JOptionPane.showMessageDialog(null,“无效输入。仅限数字。\n\n最小顺序=1\n最大顺序=100”,“摄影巷”,JOptionPane.ERROR\u消息); } }而(数量100),;,java,swing,Java,Swing,catch(NumberFormatException e)如何在我的代码中澄清变量e?在我的代码中,程序声明不使用变量e。那么我如何使用它呢?你可以使用正则表达式\\d+将匹配连续数字。另外,我建议使用do while循环。差不多 do{ try{ input = (String) JOptionPane.showInputDialog(null,"Food quantity","Dope Alley",JOptionPane.QUESTION_MES

catch(NumberFormatException e)如何在我的代码中澄清变量e?在我的代码中,程序声明不使用变量e。那么我如何使用它呢?你可以使用正则表达式<代码>\\d+将匹配连续数字。另外,我建议使用
do while
循环。差不多

do{
         try{
            input = (String) JOptionPane.showInputDialog(null,"Food quantity","Dope Alley",JOptionPane.QUESTION_MESSAGE);
            quantity = Integer.parseInt(input);
            if(quantity <= 0 || quantity > 100){
                JOptionPane.showMessageDialog(null,"Invalid input . Number only .\n\nMininum order = 1\nMaximum order = 100","Dope Alley",JOptionPane.ERROR_MESSAGE);
            }
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null,"Invalid input . Number only .\n\nMininum order = 1\nMaximum order = 100","Dope Alley",JOptionPane.ERROR_MESSAGE);
        }
    }while(quantity <= 0 || quantity > 100);
int数量=-1;
做{
输入=(字符串)JOptionPane.showInputDialog(null,“食物数量”,
“毒品巷”,JOptionPane。问题消息);
如果(输入。匹配(\\d+)){
数量=整数.parseInt(输入);
}
}而(数量<1 | |数量>100);

也许
“\\d{1,8}”
可以防止smarty裤子溢出:)如果数量是-1,我想如果用户输入0,它会proceed@ZaemShakkir不,因为
0
-1
小于
1
。好的,我试试看。那么我想如何让用户输入为0,然后显示错误消息您可以保留循环运行次数的另一个计数,如果不是第一次,则显示错误消息。
int quantity = -1;
do {
    input = (String) JOptionPane.showInputDialog(null, "Food quantity",
            "Dope Alley", JOptionPane.QUESTION_MESSAGE);
    if (input.matches("\\d+")) {
        quantity = Integer.parseInt(input);
    }
} while(quantity < 1 || quantity > 100);