1

みんな JOptionPane を使用しようとしていますが、間違った入力値を入力したかのようにキャンセル ボタンが応答し、プログラムを終了しません。どんなアイデアもとても役に立ちます!

int n = 0, k = 0;

Students stu = new Students();      

while (n <= 0) { 

     try { 
       n = Integer.parseInt(JOptionPane.showInputDialog(stu, "Enter the number of people","Input", JOptionPane.INFORMATION_MESSAGE));

       if (n <= 0) {
        OptionPane.showMessageDialog(stu, "You have given a wrong input!", 
             "Warning", JOptionPane.WARNING_MESSAGE);
       }    
    }

    catch (Exception e) { 
          JOptionPane.showMessageDialog(stu, "You have given a wrong input!",
               "Warning",  JOptionPane.WARNING_MESSAGE);
          n = 0; 
    }
}
4

3 に答える 3

0

JOptionPane補足として、スペルミス(11 行目) は不必要に例外を発生させるべきだと思います。

于 2013-06-03T11:59:44.570 に答える