Java Try-Catch异常计算器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22070535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:23:30  来源:igfitidea点击:

Java Try-Catch Exception Calculator

javaexception-handlingtry-catchjava.util.scanner

提问by user3360708

So I'm trying to make a user input calculator using try-catchand exception. The program keeps on repeating and couldn't get the actual input for the numbers and it also includes the statement Wrong input. Please try again.

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

Any idea on how to fix this?

关于如何解决这个问题的任何想法?

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);
    } 
}

采纳答案by AJ.

You need to do this

你需要这样做

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

because you are reading next input with readline()

因为你正在阅读下一个输入 readline()

String dimension = scan.nextLine();

and \nis already present it the stream when you enter your option with nextInt()because nextint()never reads the \nwhich you leave behind by pressing Enterbutton.

并且\n在您输入选项时已经将其呈现在流中,nextInt()因为nextint()永远不会读取\n您按下Enter按钮留下的内容。

回答by Leos Literak

This will help you to identify root cause:

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

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

回答by Marcin Skiba

You should add:

你应该添加:

scan.skip("\n");

right after

紧随其后

int choice = scan.nextInt();

Why? Because nextInt() reads next integer from input. And it leaves new line character at the end on the input. It's like:

为什么?因为 nextInt() 从输入中读取下一个整数。它在输入的末尾留下换行符。就像是:

1<enter>

After that you invoke scan.nextLine()which reads everything up to next new line character. In fact there is 1 new line character in the buffer. And here your nextLine() reads an empty string.

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

To solve that issue you have to skip buffered new line characters with scan.skip("\n").

要解决该问题,您必须使用scan.skip("\n").

EDIT: in addition, your code doesn't allow you to exit, because you're not changing your xvariable. Try to change it after System.out.println("Now exiting program...");. It will work ;)

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