Java Try-Catch异常计算器

时间:2014-02-27 13:50:27

标签: java exception-handling try-catch java.util.scanner

所以我正在尝试使用 try-catch 异常制作用户输入计算器。程序不断重复,无法获得数字的实际输入,还包括语句Wrong input. Please try again

有关如何解决这个问题的想法吗?

import java.util.*;
public class Calculator
{
    public static void main(String[] args) {
        int x=1;
        do {
            try {
                System.out.println("Menu");
                System.out.println("1-Addition");
                System.out.println("2-Subtraction");
                System.out.println("3-Multiplication");
                System.out.println("4-Divison");
                System.out.println("5-Modulos");
                System.out.println("6-Exit");
                System.out.println("Choose option: ");
                Scanner scan = new Scanner(System.in);
                int choice = scan.nextInt();  


                switch (choice) {
                    case 1: 
                        System.out.print("Input two numbers:");
                        String dimension = scan.nextLine();
                                String[] parts = dimension.split(" ");
                                int a = Integer.parseInt(parts[0]);
                                int b = Integer.parseInt(parts[1]);
                                int c=a+b;
                                System.out.println("Sum = " +c);
                            break;  
                    case 2: 
                        System.out.print("Input two numbers:");
                        String dif = scan.nextLine();
                                String[] difference = dif.split(" ");
                                int num1 = Integer.parseInt(difference[0]);
                                int num2 = Integer.parseInt(difference[1]);
                                    int d=num1-num2;
                                    System.out.println("Difference = " +d);
                        break;
                    case 3: 
                        System.out.print("Input two numbers:");
                        String multi = scan.nextLine();
                                String[] product = multi.split(" ");
                                int num3 = Integer.parseInt(product[0]);
                                int num4 = Integer.parseInt(product[1]);
                                int p=num3*num4;
                                System.out.println("Product = " +p);
                        break;  
                    case 4: 
                        System.out.print("Input two numbers:");
                        String div = scan.nextLine();
                                String[] quotient = div.split(" ");
                                int num5 = Integer.parseInt(quotient[0]);
                                int num6 = Integer.parseInt(quotient[1]);
                                int q=num5/num6;
                                System.out.println("Quotient = " +q);
                         break;
                    case 5: 
                        System.out.print("Input two numbers:");
                        String mod = scan.nextLine();
                                String[] modulo = mod.split(" ");
                                int num7 = Integer.parseInt(modulo[0]);
                                int num8 = Integer.parseInt(modulo[1]);
                                int m=num7%num8;
                                System.out.println("Modulos = " +m);
                        break;
                    case 6: 
                        System.out.println("Now exiting program...");
                        break;  
                }
            } catch (Exception e) {
               System.out.println("Wrong input. Try again.");
            }
        } while (x==1);
    } 
}

3 个答案:

答案 0 :(得分:3)

你需要这样做

int choice = Integer.parseInt(scan.nextLine());

因为您正在使用readline()

阅读下一个输入
String dimension = scan.nextLine();
当您使用\n输入选项时,

nextInt()已经显示在该流中,因为nextint()永远不会通过按\n来读取您留下的Enter }按钮。

答案 1 :(得分:2)

你应该添加:

scan.skip("\n");

之后

int choice = scan.nextInt();

为什么呢?因为nextInt()从输入读取下一个整数。并且它在输入的末尾留下新的行字符。就像:

1<enter>

之后,您调用scan.nextLine(),它会读取下一个新行字符的所有内容。实际上缓冲区中有1个新行字符。在这里你的nextLine()读取一个空字符串。

要解决该问题,您必须使用scan.skip("\n")跳过缓冲的换行符。

编辑: 此外,您的代码不允许您退出,因为您没有更改x变量。尝试在System.out.println("Now exiting program...");之后更改它。它会工作;)

答案 2 :(得分:1)

这将帮助您确定根本原因:

} catch (Exception e) {
    System.out.println("Wrong input. Try again.");
    e.printStackTrace();
}