无论条件如何,我的银行错误消息都会出现,并且不接受用户输入

时间:2018-03-06 00:20:13

标签: java

如果用户输入的数字大于其银行余额,我很难确定选择2 的错误消息。然而,即使用户输入的金额小于其银行余额,也会出现。现在更多地使用它已经使我的问题变得更糟,因为用户必须不止一次输入答案才能得到回复。如果还有关于问题的事情还不清楚,请告诉我。这是我的第一个编程课程,我还没有自己找出解决方案,所以任何帮助都会非常受欢迎。

//Part 2
            double balance = 0;
            BankAccount myBankAccount=new BankAccount(500.00);

        myBankAccount.deposit(1000.00);
        System.out.println(myBankAccount.getBalance());
        myBankAccount.withdrawal(600.00);
        System.out.println(myBankAccount.getBalance());

        //Part 3
        Scanner input=new Scanner(System.in);

        int choice;
        System.out.println("Welcome to M&T Bank!");
        System.out.println("Select a Banking Option Below");
        myBankAccount.menu();
        choice=input.nextInt();
        while(choice<4&&choice>0)
        {
            if(choice==1)
            {
                System.out.println("How much will you deposit?");
                myBankAccount.deposit(input.nextDouble());
            }
            else if(choice==2)
            {
                System.out.println("How much will you withdraw?");
                if(input.nextDouble()>balance)
                {
                    while(input.nextDouble()>balance)
                    {
                        System.out.println("Your account has insufficient funds. Your balance is: "+myBankAccount.getBalance());
                        System.out.println("How much will you withdraw?");
                        myBankAccount.withdrawal(input.nextDouble());
                    }
                }
                else
                {
                    myBankAccount.withdrawal(input.nextDouble());
                }
            }
            else if(choice==3)
            {
                System.out.println("Your balance is: "+myBankAccount.getBalance());
            }
            System.out.println("Welcome to M&T Bank!");
            System.out.println("Select a Banking Option Below");
            myBankAccount.menu();
            choice=input.nextInt();
        }
        if(choice==4)
        {
            System.out.println("Thank you! Goodbye.");
        }

2 个答案:

答案 0 :(得分:0)

每次拨打input.nextDouble()时,您都在阅读其他输入内容。此外,无论余额如何,您都会退出。此外,balance为0.试试这个:

else if (choice == 2) {
    System.out.println("How much will you withdraw?");
    double amount;
    while ((amount = input.nextDouble()) > myBankAccount.getBalance()) {
        System.out.println("Your account has insufficient funds. Your balance is: "+myBankAccount.getBalance());
        System.out.println("How much will you withdraw?");
    }
    myBankAccount.withdrawal(amount);
}

答案 1 :(得分:0)

    BankAccount myBankAccount = new BankAccount(500.00);

    myBankAccount.deposit(1000.00);
    System.out.println(myBankAccount.getBalance());
    myBankAccount.withdrawal(600.00);
    System.out.println(myBankAccount.getBalance());

    Scanner input=new Scanner(System.in);
    int choice = input.nextInt();

    while (choice > 4 || choice < 1){
    Scanner input=new Scanner(System.in);
    int choice = input.nextInt();}


    System.out.println("Welcome to M&T Bank!");
    System.out.println("Select a Banking Option Below");
    myBankAccount.menu();

    Switch(choice){
    case 1:{
           System.out.println("How much will you deposit?");
           myBankAccount.deposit(input.nextDouble());}

    case 2:{
            System.out.println("How much will you withdraw?");
            while(input.nextDouble() > myBankAccount.getBalance(){
            System.out.println("Your account has insufficient funds.");
            System.out.println("Your balance is: "+myBankAccount.getBalance());
            System.out.println("How much will you withdraw?");}
            myBankAccount.withdrawal(input.nextDouble());
            }

    case 3: System.out.println("Your balance is: "+myBankAccount.getBalance());
    case 4 : System.out.println("Thank you! Goodbye.");