java - 当用户输入小数点时,如何错误处理代码以防止程序崩溃?

标签 java double decimal banking

这是在java中,我的问题是,当用户输入“depositAmount”时,如果他们使用小数点或逗号,程序就会崩溃,我知道他们的输入需要是 double 类型,但我不知道怎么说“如果用户的输入是 double 类型,接受它,否则,要求新输入”

import java.util.Scanner;
import java.util.ArrayList;
public class Engine

{

private static ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();

public static void Engine()
{

    Scanner Reader = new Scanner(System.in);
    BankAccount n = new BankAccount();
    boolean keepGoing = true;

    while(keepGoing)
    {
        System.out.println("Welcome to The Bank of Money, what would you like to do?\n enter n to create a new account, enter e to use an existing account, or enter q to quit to main menu");
        String response = Reader.nextLine();
        if(response.equals("q")) keepGoing = false;
        else if(response.equals("n")) 
        {
            accounts.add(new BankAccount());
            System.out.println("Your account number is: " + accounts.get(accounts.size()-1).getAccountNum());
        }
        else if(response.equals("e"))
        {
            System.out.println("what is your account number?");
            Scanner Reader1 = new Scanner (System.in);
            int accountNum = Reader1.nextInt();  
            BankAccount selectedAccount = null;
            for(int i = 0; i<accounts.size();i++)
            {
                if (accounts.get(i).getAccountNum()==accountNum)
                {
                    selectedAccount = accounts.get(i);

                }
            }
            if(selectedAccount ==null)System.out.println("There is no such account. Please try again.");
            else transact(selectedAccount);
        }
    }
}

public static void transact(BankAccount selectedAccount)
{
    Scanner in = new Scanner(System.in);
    int response = 0;
    while(response!=7)
    {
        BankAccount.printTransactionMenu();
        response = in.nextInt();
        if(response==1)
        {
            System.out.println("Enter the amount that you would like to deposit");
            System.out.print("$");
            Scanner depositScanner = new Scanner(System.in);
            double depositAmount = depositScanner.nextInt();
        }
        if(response==2)
        {
            System.out.println("Enter the amount that you would like to withdraw");
            System.out.print("$");
            Scanner withdrawScanner = new Scanner(System.in);
            double withdrawAmount = withdrawScanner.nextInt();
            if(withdrawAmount>selectedAccount.getBalance()) 
                System.out.println("Insufficient funds");
            else selectedAccount.withdraw(withdrawAmount);
        }
        if(response==3)
        {
            selectedAccount.assessInterest();
            System.out.println("Your new balance is $" + selectedAccount.getBalance());
        }
        if(response==4)
        {
            System.out.println("Your account balance is $" + selectedAccount.getBalance());
        }
        if(response==5)
        {
            System.out.println("Which account would you like to transact with?");
            Scanner Reader1 = new Scanner (System.in);
            int transactAccountNum = Reader1.nextInt();  
            BankAccount transactAccount = null;
            for(int i = 0; i<accounts.size();i++)
            {
                if (accounts.get(i).getAccountNum()==transactAccountNum)
                {
                    transactAccount = accounts.get(i);
                }
            }
            if(transactAccount ==null)System.out.println("There is no such account. Please try again.");
            else 
            {   
                System.out.println("What amount would you like to transfer?");

                Scanner transferScanner = new Scanner(System.in);
                double transactAmount = transferScanner.nextDouble();
                selectedAccount.transfer(transactAccount, transactAmount);
            }
        }
        if(response==6)
        {
            selectedAccount.printStatement();
        }
    }
}

}

最佳答案

您可以使用 next() 方法从 Scanner 获取最初没有 int 或 double 值的输入。如果您知道数据类型,则可以检查它是否具有下一个 double 值 (hasNextDouble) 或您需要的任何数据类型。如果没有,您可以接受 String 类型并对其进行转换。

Scanner in = new Scanner(System.in);
String s = in.next();

从那里您可以将其解析为适当的数据类型(例如 double )。

double d = Double.parseDouble(s);

我希望这有帮助!

关于java - 当用户输入小数点时,如何错误处理代码以防止程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26225965/

相关文章:

c++ - 从 OpenCV Mat 中获取值(value)

javascript - javascript 将变量舍入为小数点后 1 位

javascript - 如何检查给定变量输入的小数点右移次数?

java - 通过分析字节码,我如何检测 catch block 中的显式 throw 语句调用?

java - Log4j2-JDBC Appender

java - 为什么我的 Spring Controller 不处理 GraphiQL 发送的 OPTIONS 请求?

java - 如何将 String ArrayList 转换为 Double ArrayList 并计算值的总和?

java - 使用 HTML 在 TextView 中设置图像上方的文本

java - 从 double 中删除一个字符

ios - 如何在iOS应用程序中将小数位限制为四位