实现try和catch块

时间:2013-10-11 10:10:12

标签: java try-catch block

我终于(差不多)完成了一个我一直在研究的小计算器项目。目前,它适用于加法,减法,乘法,除法,mod,平方和否定双精度,并响应一些字符串,如“退出”,“清除”和“帮助”。

我只需要帮助实现try和catch块,以便在控制台输入“输入第一个数字”或“输入第二个数字”后,只要用户输入非数字的任何内容,就会在控制台中返回消息,例如“无效输入” ”

我用几种不同的方法搞砸了,但似乎没有任何工作。建议将不胜感激。 代码:

import java.util.Scanner;


public class exit {

    static double num1;
    static double num2;
    static String operation;

    public static void main(String[] args) {

        boolean run = true;

        while (run) {

            System.out.println(" Enter: \n \b help for all usable operations \n continue to use the    calculator");
            Scanner input = new Scanner(System.in);
            String str1 = input.next();

            if (str1.equals("exit")) {

                System.exit(0);
            } else if (str1.equals("clear")) {

                System.out.println("0.0");
            } else if (str1.equals("help")) {
                System.out.println("please enter:\n + for addition \n - for subtraction \n * for     multiplication \n / for division \n -x for negation \n x^2 for squaring\n % for mod \n  remember to only input integer or double values\n");

            } else if (str1.equals("continue")) {

                System.out.println("\nEnter the first number:");
                num1 = input.nextInt();


                System.out.println
                        ("Display:" + num1);

                System.out.println("Please enter operation:");
                operation = input.next();


                System.out.println("Enter the second number:");
                num2 = input.nextInt();
                System.out.println("Display:" + num2);


                if ("+".equals(operation)) {

                    System.out.println((num1 + num2));
                } else if ("-".equals(operation)) {

                    System.out.println((num1 - num2));
                } else if ("/".equals(operation)) {

                    System.out.println((num1 / num2));
                } else if ("*".equals(operation)) {

                    System.out.println((num1 * num2));
                } else if ("-x".equals(operation)) {

                    System.out.println(-num1);
                } else if ("x^2".equals(operation)) {

                    System.out.println(num1 * num1);
                } else if ("sqrt".equals(operation)) {

                    System.out.println(Math.sqrt(num1));

                }


            }

        }
    }
}

2 个答案:

答案 0 :(得分:3)

要通过实施try-catch检查号码的有效性,您可以尝试执行以下操作:

try{
    Double num = Double.parseDouble(input.nextLine()); // This is just a sample
    // int someInt = Integer.parseInt(someBasString); // Even this will throw NFE
}catch(NumberFormatException NFE){ // If its not a valid number, you'll get this exception
    // Do something on error
}

答案 1 :(得分:0)

您可以使用此

try{
num1 = input.nextInt();
..
num2 = input.nextInt();
}
catch(Exception ex)
{
  System.out.println("Invalid input")
}

要在捕获'Exception'之前使其更好,您可以捕获'InputMismatchException'或您期望的任何其他异常。这样您就可以从catch中显示更相关的信息。

相关问题