繁体   English   中英

使用JOptionPane进行用户输入不正确的循环

[英]loop while user input is incorrect using JOptionPane

我正在尝试用Java模拟彩票游戏。 现在一切正常,我正在验证用户只输入数字1到100>我也想防止用户输入空值。 目前正在使用try .. catch ..进行工作,但仅在第一次使用。 用户输入6个数字。 假设第一个输入为空白,则显示错误,但如果用户再次在空输入上按Enter键,程序将崩溃。 我无法使它循环我已经尝试了好几件事,但是没有运气。 这是我获得用户输入的代码。

String [] charNums = {"1st","2nd","3rd","4th","5th","Bonus"};
//get input 5 numbers from user 
for(int i=0; i<6;i++){

    //boolean correctInput = false;
    //while(!correctInput){
         try {
            inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                while(!validate(inputNumbers[i])){
                     JOptionPane.showMessageDialog(null,"Invalid Number! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                     inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                }
                ////check for duplicate entries from user
                for (int k=0; k<i; k++)  {    
                    while (k!=i && inputNumbers[k] == inputNumbers[i])  {
                        JOptionPane.showMessageDialog(null,"Duplicate Entry! try again","ERROR",JOptionPane.ERROR_MESSAGE);
                        inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                    }
                }
                //correctInput = true;
                //break;
            }catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null,"Number not entered! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                //throw new NumberFormatException("not number");
                inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                //correctInput = false;
                //JOptionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                //continue;

            //}
        }
        userNumbers[i] = inputNumbers[i];
    }

尝试做/做

 boolean correctInput = false;
        ///create array to display user
        String [] charNums = {"1st","2nd","3rd","4th","5th","Bonus"};
        //get input 5 numbers from user 
        for(int i=0; i<6;i++){
            do {
                try {
                    inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                    while(!validate(inputNumbers[i])){
                        JOptionPane.showMessageDialog(null,"Invalid Number! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                        inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                    }

                    ////check for duplicate entries from user
                    for (int k=0; k<i; k++)  {    
                        while (k!=i && inputNumbers[k] == inputNumbers[i])  {
                            JOptionPane.showMessageDialog(null,"Duplicate Entry! try again","ERROR",JOptionPane.ERROR_MESSAGE);
                            inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                        }
                    }
                    correctInput = true;
                    //break;
                }catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null,"Number not entered! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                    //throw new NumberFormatException("not number");
                    inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                    correctInput = false;
                    //JOptionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                    //continue;

                }
            }while(!correctInput);
            userNumbers[i] = inputNumbers[i];
        }

try / catch应该在while循环内,否则抛出异常时退出循环。

提示:做/做。

这是对您的代码进行了修改,使其更容易遍历6个数字,我已经对其进行了测试,并且效果很好。 让我知道是否不清楚

   String [] charNums = {"1st","2nd","3rd","4th","5th","Bonus"};
   int[] inputNumbers = new int[6];
   //get input 6 numbers from user 
   int cnt = 0;
   while(true)
   {
     try
     {
       String tmp = JOptionPane.showInputDialog("Enter "+charNums[cnt]+" number from 1 to 100");
       int val = Integer.parseInt(tmp);
       boolean duplicate = false;
       for (int k=0; k<cnt; k++)
       {
         if(val==inputNumbers[k])
         {
           JOptionPane.showMessageDialog(null,"Duplicate Entry! try again","ERROR",JOptionPane.ERROR_MESSAGE);
           duplicate = true;
           break;
         }   
       }   
       if(duplicate)continue;

       inputNumbers[cnt] = val;
       cnt++;
     }
     catch(Exception e)
     {
     }
     if(cnt==6)break;
   }

上面的代码对我不起作用。 但是我通过将try and catch放入validate方法中来解决,现在每次输入错误时它就会循环。 这是代码

public static boolean validate(String num){

    try {
        int convertedNum = Integer.parseInt(num);
        if(convertedNum < 0 || convertedNum > 100){
            return false;
        }else{
            return true;
        }
    }catch ( NumberFormatException e){
        return false;
    }
}

for(int i=0; i<6;i++){

            inputNumbers[i] = JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100");
            while(!validate(inputNumbers[i])){
                if(inputNumbers[i] == null){
                    int stopGame = JOptionPane.showConfirmDialog(null,"Do you wish to cancel the game? All progress will be lost","",JOptionPane.YES_NO_OPTION);
                    if(stopGame == JOptionPane.YES_OPTION){
                        System.exit(0);
                    }
                }
                JOptionPane.showMessageDialog(null,"Invalid Number! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                inputNumbers[i] = JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100");
            }

            ////check for duplicate entries from user
            for (int k=0; k<i; k++)  {    
                while (k!=i && Integer.parseInt(inputNumbers[k]) == Integer.parseInt(inputNumbers[i]))  {
                    JOptionPane.showMessageDialog(null,"Duplicate Entry! try again","ERROR",JOptionPane.ERROR_MESSAGE);
                    inputNumbers[i] = JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100");
                }
            }

            //convert string to int
            userNumbers[i] = Integer.parseInt(inputNumbers[i]);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM