2015-10-04 115 views
0

我一直在編寫一個銀行業務程序,但我在退出後更改當前餘額時遇到問題。它第一次運作,但第二次回到原始金額。就像我有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); 
    } 
} 

回答

0

好,任何時候你取錢,你初始化平衡回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

0

刪除此行:

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

您的答案被標記爲低質量。您應該在回答中添加一些精確度,以及爲什麼它應該解決問題 – Dici

0

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