使用Java计算器为无效输入创建错误消息

时间:2015-09-24 15:19:39

标签: java calculator

我必须用Java创建一个计算器。到目前为止一切正常,除非当有人在请求数字时输入一个不是整数的值时,我无法弄清楚如何产生错误消息。我尝试了一个“try,catch”语句,但它仍然在IDE中引发了一个错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)

这是我程序其余部分的代码:

import java.util.InputMismatchException;
import java.util.Scanner;
public class Calculator {

static int num1, num2;
static int memory;
static String operation;
static String menu = "\nChoose an operation:\n+ Add\n- Subtract\n* Multiply\n/ Divide\n^ Exponent\n~ Square Root\n Exit";
static boolean run = true;

public static void menu (String menu){
    System.out.println (menu);
};

public static void add (int num1, int num2) {
    System.out.println (num1 +num2);}

public static void subtract (int num1, int num2) {
    System.out.println (num1 - num2);           
};

public static void multiply (int num1, int num2) {
    System.out.println (num1 * num2);           
};

public static void divide (int num1, int num2) {
    if (num2 !=0) {
        System.out.println (num1/num2);}
    else{
        System.out.println ("Cannot divide by 0");
    };
};

public static void exp (int num1, int num2) {
    System.out.println (Math.pow(num1,num2));           
};

public static void sqrt (int num1) {
    System.out.println (Math.sqrt(num1));           
};

public static void main(String[] args) {
    Scanner scanner = new Scanner (System.in);

    do{
        menu(menu);
        operation = scanner.next();

        try {
        switch (operation) {
            case "+":
                System.out.println ("Enter first number:");
                num1 = scanner.nextInt();
                System.out.println ("Please enter a valid number");
                System.out.println ("Enter second number:");
                num2 = scanner.nextInt();
                add (num1,num2);    
                break;
            case "-":
                System.out.println ("Enter first number:");
                num1 = scanner.nextInt();
                System.out.println ("Enter second number:");
                num2 = scanner.nextInt();
                subtract (num1, num2);
                break;
            case "*":
                System.out.println ("Enter first number:");
                num1 = scanner.nextInt();   
                System.out.println ("Enter second number:");
                num2 = scanner.nextInt();
                multiply (num1, num2);
                break;
            case "/":
                System.out.println ("Enter first number:");
                num1 = scanner.nextInt();
                System.out.println ("Enter second number:");
                num2 = scanner.nextInt();
                divide (num1, num2);
                break;
            case "^":
                System.out.println ("Enter first number:");
                num1 = scanner.nextInt();
                System.out.println ("Enter exponent:");
                num2 = scanner.nextInt();
                exp (num1, num2);
                break;
            case "~":
                System.out.println ("Enter number:");
                num1 = scanner.nextInt();
                sqrt(num1);
                break;
            case "Exit":
                System.out.println("You have exited the calculator");
                System.exit(0);
                run = false;
                break;
            default:
                System.out.println("Invalid");
            }
        }catch (InputMismatchException e) {

        }
    }while(run == true);



    scanner.close();
        }

};

谢谢!

ok更新:我得到了try catch来抛出错误,但是当我运行它时出现错误,它运行switch语句的默认情况,然后再次运行(所以你看两次菜单)我只需要它出现一次

4 个答案:

答案 0 :(得分:1)

使用try catch块包装整个方法。您的try / catch块已本地化为单个区域,它只会在该区域中抛出异常。

答案 1 :(得分:0)

您正在使用Scanner.nextInt()方法。当看到无法转换为int的输入时,它将抛出InputMismatchException。

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

首先检查“hasNextInt()”,可以验证下一个输入是int类型。

答案 2 :(得分:0)

在尝试乘法时看起来你的错误被抛出(case" *")......它在try / catch之外,因此你会得到这个错误。尝试/捕获每个输入或将整个事物包装成一个单一的。

答案 3 :(得分:0)

case "+":
                System.out.println ("Enter first number:");
                while(!scanner.hasNextInt())
                {
                    System.err.println("Enter valid int");
                    scanner.next();
                }

                num1 = scanner.nextInt();
                System.out.println ("Enter second number:");
                while(!scanner.hasNextInt())
                {
                    System.err.println("Enter valid int");
                    scanner.next();
                }

                num2 = scanner.nextInt();
                add(num1, num2);

                break;

使用类似这样的东西而不是try catch块。通过这种方式,它会一直有效,直到您获得有效的num1。同样也适用于其他号码。