如何处理Java中的无效输入

时间:2019-04-29 07:02:13

标签: java

我正在制作一个简单的银行存款和提款代码。代码适用于“存款”部分,但在提款时会要求提款2次。并且取取取值的最后一个值。

我认为我需要在某个地方输入scannerObject.nextLine();,但是在哪里以及如何使用scannerObject.nextLine();

以下是我的示例代码。还有另一个类文件BankAccount.java,仅具有getter和setter方法。

package com.amit;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount();
        boolean option = true;
        Scanner scanner = new Scanner(System.in);
        while (option){
            System.out.println("Press 1 For Deposite. Press 2 For Withdrawal. Press 3 For Exit");
            boolean hasvalue = scanner.hasNextInt();
            if(hasvalue){
                //means user has entered integer value now check if its in 1, 2 if  its other
                // than this we'll take him out of program to print balance
                int userValEntered = scanner.nextInt();
                if (userValEntered == 1){
                    //code for deposite
                    System.out.println("Enter Amount To Deposite");
                    Scanner amountToDeposite = new Scanner(System.in);
                    account.setBalance(amountToDeposite.nextDouble());
                }else if (userValEntered == 2) {
                    //Code for withdrawal
                    System.out.println("Enter Amount To Withdraw");
                    Scanner amountToWithdraw = new Scanner(System.in);
                    if (amountToWithdraw.nextDouble() >= account.getBalance()){
                        System.out.println("Unable to Withdraw Given Amount, Try Other Amount");
                        continue;
                    }else {
                        double currentBalance = account.getBalance() - amountToWithdraw.nextDouble();
                        account.setBalance(currentBalance);
                        System.out.println("Thanks for Doing Business With us");
                    }
                }else{
                    //if user enters anything other than 1 or 2
                    break;
                }
            }else {
                //if user enters anything other than integer
                break;
            }

        }
        //code to print balance here.
        System.out.println("Your Balance is: "+account.getBalance());
    }
}

1 个答案:

答案 0 :(得分:0)

由于@ Tiij7,我发现我曾经两次使用.nextDouble的错误。从现在开始,我将确保在使用它之前先将其保存在变量中。 这就是我更新代码的方式。

package com.amit;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount();
        boolean option = true;
        Scanner scanner = new Scanner(System.in);
        while (option){
            System.out.println("Press 1 For Deposite. Press 2 For Withdrawal. Press 3 For Exit");
            boolean hasvalue = scanner.hasNextInt();
            if(hasvalue){
                //means user has entered integer value now check if its in 1, 2 if  its other
                // than this we'll take him out of program to print balance
                int userValEntered = scanner.nextInt();
                if (userValEntered == 1){
                    //code for deposite
                    System.out.println("Enter Amount To Deposite");
                    Scanner amountToDeposite = new Scanner(System.in);
                    account.setBalance(amountToDeposite.nextDouble());
                }else if (userValEntered == 2) {
                    //Code for withdrawal
                    System.out.println("Enter Amount To Withdraw");
                    Scanner amountToWithdraw = new Scanner(System.in);
                    double withdrawAmt = amountToWithdraw.nextDouble();
                    if (withdrawAmt >= account.getBalance()){
                        System.out.println("Unable to Withdraw Given Amount, Try Other Amount");
                        continue;
                    }else {
                        double currentBalance = account.getBalance() - withdrawAmt;
                        account.setBalance(currentBalance);
                        System.out.println("Thanks for Doing Business With us");
                    }
                }else{
                    //if user enters anything other than 1 or 2
                    break;
                }

            }else {
                //if user enters anything other than integer
                break;
            }

        }
        //code to print balance here.
        System.out.println("Your Balance is: "+account.getBalance());
    }
}