【问题标题】:Implementing a try and catch block实现一个 try and catch 块
【发布时间】:2013-10-19 09:08:43
【问题描述】:

我终于(几乎)完成了我一直在做的一个小计算器项目。目前,它适用于双精度数的加法、减法、乘法、除法、取模、平方和否定,并响应一些字符串,例如“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));

                }


            }

        }
    }
}

【问题讨论】:

标签: java try-catch block


【解决方案1】:

要通过实现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
}

【讨论】:

    【解决方案2】:

    你可以用这个

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多