为什么这个 try 语句使用 catch 语句?[已结束]

Why does this try statement use the catch statement? [closed]

提问人:codenaem 提问时间:11/21/2020 更新时间:11/21/2020 访问量:49

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将有助于其他人回答这个问题。

3年前关闭。

import java.util.Scanner;
import java.util.InputMismatchException;
public class bank {
    public static void login() {
        double balance;
        try {
            boolean subtract;
            boolean amounttaken;
            // This is to see if you want to withdraw money
            System.out.println("Do you want to withdraw money?");
            Scanner SCaddOrWithdraw = new Scanner(System.in);
            subtract = SCaddOrWithdraw.nextBoolean();
            if (subtract) {
                System.out.println("How much would you like to withdraw?");
                Scanner SCamounttaken = new Scanner(System.in);
                amounttaken = SCamounttaken.nextBoolean();
                System.out.println("Subtract");
            } else if (!subtract) {
                System.out.print("Ask for bal");
            }
        } catch (InputMismatchException e) {
            System.out.println("Invalid Input");
        }
    }
}

我添加了一个 try 语句,在 if 我告诉它打印减法的第一部分,但它调用了 catch 语句。有人可以帮忙吗? 请记住,我是编码的初学者。

java try-catch java.util.scanner 输入MismatchException

评论

3赞 JustAnotherDeveloper 11/21/2020
请提供您用作输入的值。

答:

1赞 Rehan Javed 11/21/2020 #1

这是一个完整的结构。

try {
    // Statement Which May Throw Exceptions
} catch(Exception e) {
    // If any statement throws the exception
    // Do alternative flow here
}

如果 try 块中的代码可能会引发异常,则可以执行替代操作。就像在上面的代码中一样,如果有人输入一个字符串而不是布尔值,它将抛出 InputMismatchException,它将被捕获在 catch 块中,您可以漂亮而整齐地将错误消息打印给用户。:)

1赞 Arvind Kumar Avinash 11/21/2020 #2

您已声明 和 作为变量,因此它们只能采用或作为值,例如下面给出的是一个示例运行:subtractamounttakenbooleantruefalse

Do you want to withdraw money?
true
How much would you like to withdraw?
true
Subtract

如果尝试输入与 或 不同的值,则会得到 。truefalseInputMismatchException

我想你想为变量输入一些十进制数(金额)。如果是,则将其声明为 或代替,例如amounttakendoublefloatboolean

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        login();
    }

    public static void login() {
        double balance;
        try {
            boolean subtract;
            double amounttaken;// Changed to double
            // This is to see if you want to withdraw money
            System.out.println("Do you want to withdraw money?");
            Scanner SCaddOrWithdraw = new Scanner(System.in);
            subtract = SCaddOrWithdraw.nextBoolean();
            if (subtract) {
                System.out.println("How much would you like to withdraw?");
                Scanner SCamounttaken = new Scanner(System.in);
                amounttaken = SCamounttaken.nextDouble();// Changed for double
                System.out.println("Subtract");
            } else if (!subtract) {
                System.out.print("Ask for bal");
            }
        } catch (InputMismatchException e) {
            System.out.println("Invalid Input");
        }
    }
}

示例运行:

Do you want to withdraw money?
true
How much would you like to withdraw?
3000
Subtract