簡體   English   中英

實施try and catch塊

[英]Implementing a try and catch block

我終於(幾乎)完成了一個我一直在從事的小計算器項目。 當前,它可以與加,減,乘,除,mod,平方和雙精度取反一起工作,並對某些字符串(例如“ exit”,“ clear”和“ help”)作出響應。

只要用戶在控制台說“輸入第一個數字”或“輸入第二個數字”之后輸入任何非數字的內容,我只需要實現一個try and catch塊來在控制台中返回一條消息,例如“ Invalid Input”。

我已經弄亂了幾種不同的方法,但似乎沒有任何效果。 建議將不勝感激。 碼:

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

                }


            }

        }
    }
}

要通過實現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
}

你可以用這個

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

為了在捕獲“異常”之前變得更好,您可以捕獲“ InputMismatchException”或您期望的任何其他異常。這樣,您可以顯示來自catch的更多相關信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM