0

我终于(几乎)完成了我一直在做的一个小计算器项目。目前,它适用于双精度数的加法、减法、乘法、除法、模数、平方和否定,并响应一些字符串,如“exit”、“clear”和“help”。

每当用户在控制台说“输入第一个数字”或“输入第二个数字”后输入任何不是数字的内容时,我只需要帮助实现一个 try and 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));

                }


            }

        }
    }
}
4

2 回答 2

3

要通过实现 a 来检查 number 的有效性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
}
于 2013-10-11T10:14:20.063 回答
0

你可以用这个

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

为了在捕获“异常”之前让它变得更好,您可以捕获“InputMismatchException”或您期望的任何其他异常。这样您就可以从捕获中显示更相关的信息。

于 2013-10-11T10:23:38.037 回答