使用银行账户的方法

问题描述:

我一直在编写一个银行业务程序,但我在退出后更改当前余额时遇到问题。它第一次运作,但第二次回到原始金额。就像我有300美元,然后撤回50美元,它会显示250美元。但是,第二次执行后,我撤回20美元,它会显示我的余额是280美元,而不是230美元。
我的程序需要两种方法加上main方法,我的当前帐户余额是300美元。还需要一个主菜单。使用银行账户的方法

我的老师说我们不能使用数组,因为我们还没有讨论它。 这是我第一次使用的不仅仅是main方法,所以我选择的方法可能是错误的。

import java.util.Scanner; 

public class BankingATM { 
    public static void main(String[] args) { 
     double withdraw = withdrawAmount(); 
     double myBalance = withdrawAmount(); 
     balanceAmount(withdraw, myBalance); 
    } 

    public static double withdrawAmount() { 
     Scanner keyboard = new Scanner(System.in); 
     int option; 
     double myBalance; 
     double withdraw = 0; 
     myBalance = 300 - withdraw; 

     do { 
      System.out.println("Please select an option:"); 
      System.out.println("Press 1 the withdraw from you account."); 
      System.out.println("Press 2 to check your balance"); 
      System.out.println("Press 3 to Exit"); 
      { 
       option = keyboard.nextInt(); 
       if (option == 1) { 
        System.out.println("User selected to withdraw"); 
        System.out.println("How much would you like to withdraw?"); 
        withdraw = keyboard.nextDouble(); 
        myBalance = 300; 
        double newBalance = myBalance - withdraw; 
        { 
         if (withdraw > 500) { 
          System.out.println("Sorry but the ATM has a limit of $500.00"); 
         } else if (withdraw > myBalance && withdraw > 500) { 
          System.out.println("Sorry your account only has $300.00"); 
         } else if (withdraw > myBalance) { 
          System.out.println("Sorry your account only has $300.00"); 
         } else if (withdraw <= myBalance) { 
          System.out.println("The machine is realeasing $" + withdraw); 
          System.out.println("Your current balance is $" + (myBalance - withdraw)); 
         } 
        } 
       } 
       if (option == 2) { 
        System.out.println("User selected to check balance"); 
        balanceAmount(withdraw, myBalance); 
       } 
       if (option == 3) { 
        System.out.println("User chose to exit"); 
        System.exit(0); 
       } 
      } 
     } while (option != 1 || option != 2 || option != 3); 
     return myBalance; 
    } 

    public static void balanceAmount(double withdraw, double myBalance) { 
     System.out.println("Your balance is" + myBalance); 
    } 
} 

好,任何时候你取钱,你初始化平衡回300:

if(option == 1) 
    { 
     System.out.println("User selected to withdraw"); 
     System.out.println("How much would you like to withdraw?"); 
     withdraw = keyboard.nextDouble(); 
     myBalance = 300; // you should remove that line. 

此外,如果你打算叫withdrawAmount()多次,你不应该声明myBalance变量该方法中的局部变量,因为每次调用该方法都会重置余额。

+0

在计算'newBalance'之前,您还应该删除'myBalance'的第二次初始化# – hotzst

+0

谢谢,但是当我删除它时,我的程序部分告诉我必须声明应查看mybalance。我应该把它放在主要方法吗?如果是这样,我将如何呼吁它? – kidmo87

删除此行:

myBalance = 300; // you should remove that line. 
+0

您的答案被标记为低质量。您应该在回答中添加一些精确度,以及为什么它应该解决问题 – Dici

在做.. while循环中,使以下的变化: //应查看mybalance = 300; //删除此行 double myBalance = myBalance - withdraw; //也分配新值myBalance