尝试/异常为整数或双精度或字符串

时间:2014-04-07 01:40:41

标签: java string integer double

如何区分整数和双精度。示例,如果我想获得整数并验证它。如果它是整数然后输出"你是正确的"别的"你错了。再试一次"。

import javax.swing.*;

public class InputExceptions {

    private static int inputInt;
    private static double inputDouble;

    public static int inputInt() {

        boolean inputOK = false;
        while (inputOK == false) {
            inputInt = Integer.parseInt(JOptionPane.showInputDialog("Enter Integer"));

            try {

                inputOK = true;
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null,"*** ERROR: VALUE ENTERED NOT INTEGER ***");

            }

        } return inputInt;
    }

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null,"*** INTEGER is correct Input: " + inputInt() + " ***");

    }

}

0 个答案:

没有答案