使用JOptionPane时用户输入不正确循环

时间:2015-04-17 22:54:57

标签: java

我正在尝试用java模拟彩票游戏。现在一切正常我正在验证用户只输入数字1到100>我还想阻止用户输入和清空值。现在正在使用try .. catch ..但它只适用于第一次。用户输入6个数字。假设第一个输入是空白然后显示错误,但如果用户再次按空输入,则程序崩溃。我无法让它循环我已经尝试了几件没有运气的东西。这是我获得用户输入的代码。

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];
    }

尝试do / while

 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];
        }

3 个答案:

答案 0 :(得分:0)

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

提示:do / while。

答案 1 :(得分:0)

这是你的代码修改,以便更简单地循环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;
   }

答案 2 :(得分:0)

上面的代码对我没用。但是我通过将try和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]);
        }