Java 为什么这个try语句使用catch语句?

Java 为什么这个try语句使用catch语句?,java,try-catch,java.util.scanner,inputmismatchexception,Java,Try Catch,Java.util.scanner,Inputmismatchexception,我添加了一个try语句,在if的第一部分,我告诉它打印subtract,但它调用catch语句。有人能帮忙吗? 请记住,我是一名编码初学者。这是一个完整的结构 import java.util.Scanner; import java.util.InputMismatchException; public class bank { public static void login() { double balance; try {

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

这是一个完整的结构

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块中的代码可能引发异常,则可以进行替代。就像在上面的代码中一样,如果有人输入字符串而不是布尔值,它将抛出InputMismatchException,该异常将被捕获到catch块中,您可以将错误消息打印给用户,并整洁地显示出来。:)

这是一个完整的结构

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块中的代码可能引发异常,则可以进行替代。就像在上面的代码中一样,如果有人输入字符串而不是布尔值,它将抛出InputMismatchException,该异常将被捕获到catch块中,您可以将错误消息打印给用户,并整洁地显示出来。:)

您已将
subtract
amounttake
声明为
boolean
变量,因此它们只能将
true
false
作为值,例如,下面给出的是一个示例运行:

try {
    // Statement Which May Throw Exceptions
} catch(Exception e) {
    // If any statement throws the exception
    // Do alternative flow here
}
如果您试图输入一个与
true
false
不同的值,您将得到
inputmaschException

我想您需要为变量输入一些十进制数(amount),
amounttake
。如果是,则将其声明为
double
float
而不是
boolean
,例如

Do you want to withdraw money?
true
How much would you like to withdraw?
true
Subtract
运行示例:

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

您已将
subtract
amounttake
声明为
boolean
变量,因此它们只能采用
true
false
作为值,例如,下面给出的是一个示例运行:

try {
    // Statement Which May Throw Exceptions
} catch(Exception e) {
    // If any statement throws the exception
    // Do alternative flow here
}
如果您试图输入一个与
true
false
不同的值,您将得到
inputmaschException

我想您需要为变量输入一些十进制数(amount),
amounttake
。如果是,则将其声明为
double
float
而不是
boolean
,例如

Do you want to withdraw money?
true
How much would you like to withdraw?
true
Subtract
运行示例:

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

请提供用作输入的值。请提供用作输入的值。