2011-08-31 59 views
1

我有一個非常長的問題。我正在創建一個銀行賬戶,並將餘額設置爲0.如果用戶選擇提取或向賬戶存入資金,餘額不會改變。我選擇顯示餘額,它仍然表示0.這可能是一個沒有道理的但我現在已經花了。這裏是我的長碼(switch語句是在我的主類和方法在一個對象類):Java不讀取用戶修改後傳遞給它的變量

public class MyProgram2{ 
public static void main(String[] args){ 
    Scanner scan = new Scanner(System.in); 
    Scanner input = new Scanner (System.in); 
    String menu, outputString, poo; 
    int option = 1; 
    int id = 0; 
    double balance = 0, amount = 0; 
    Account acc = new Account(); 


    menu ="\n\t1 Create Account and ID" + 
      "\n\t2 Check balance" + 
      "\n\t3 Withdraw" + 
      "\n\t4 Deposit" + 
      "\n\t5 Get account ID" + 
      "\n\t6 Display Account Info" + 

      "\n\t0 Quit\n\n\n"; 

    System.out.println(menu); 
    System.out.println("\tEnter your selection: "); 
    option = scan.nextInt(); 

    while (option != 0) { 

      switch (option) { 

      case 1: //Create an account and set ID 
         System.out.print("Enter Your Account ID to create account:\t"); 
         id = input.nextInt(); 
         System.out.println("Account created!"); 
         break; 

      case 2:  //check balance 
         acc.checkBalance(balance); 
         break; 

      case 3:  //withdraw money 
         acc.withdraw(balance, amount); 
         break; 

      case 4: //deposit money 
         acc.deposit(balance, amount); 
         break; 

      case 5:  //get account id 
         acc.getID(id); 
         break; 

      case 7:  //display account info 
         System.out.print("option 7"); 
         break; 

      default: outputString = "\nInvalid Selection\n"; 
         System.out.println(outputString); 
         break; 

     } 
    System.out.println(menu); 
    System.out.print("\tEnter your selection: "); 
    option = scan.nextInt();  
    } 

這些都是我打電話的方法:

public class Account{ 
Scanner input = new Scanner (System.in); 

public Account(){ 
} 

public void getID(int id){ 
    System.out.println("Your account ID is:\t" + id); 
} 

public void checkBalance(double balance){ 
    System.out.println("Your balance is:\t$" + balance); 
} 

public double withdraw(double amount, double balance){ 
    System.out.println("How much do you want to withdraw?:\t$"); 
    amount = input.nextDouble(); 
    balance -= amount; 
    return balance; 
} 

public double deposit(double amount, double balance){ 
    System.out.println("How much do you want to deposit?:\t"); 
    amount = input.nextDouble(); 
    balance += amount; 
    return balance; 
} 

public void getAccountInfo(int id, double balance){ 

} 
} 

回答

5

變量

double balance
不能作爲參考進行傳遞。它會複製副本,所以當您嘗試操作它時,它不會影響您作爲參數傳遞的原始副本。您需要使用函數中的返回值更新該值。

爲了使其工作,你應該做的:


case 4: //deposit money 

// note here that you need to update the balance variable using the return value that 
// you put in the function 
balance = acc.deposit(balance, amount); 
break; 

注意:您的設計分離Account類餘額不按@ Psyrus的答案理想。您應該將餘額作爲Account類的一部分。原因在於餘額是賬戶的一部分,如果您的程序增長以處理多個賬戶(僅僅是爲了舉例),從賬戶中分離可變餘額會造成維護頭痛(想象一下,使用兩個賬戶實例,您將在MyProgram2,主應用程序中有balance1和balance2變量(或任何您將調用它的變量)。雖然我提供了有關變量傳遞的問題原因,但您應該重構代碼以遵循@Psyrus建議。

+1

另一種方法是,如果需要傳遞參考效果,可以創建一個帶有單個公共字段的「DoubleHolder」對象。 +1爲了使這個絕對清楚。 –

+0

我已經退回餘額...應該改變什麼? – Josh

+1

您正在返回餘額,但完全沒有使用返回值,因此該值沒有更新。我將用示例代碼 – momo

2

你只需要修改你喜歡這個創建的實例:

public double deposit(){ 
    System.out.println("How much do you want to deposit?:\t"); 
    this.amount = input.nextDouble(); 
    this.balance += amount; 
    return balance; 
} 

this keyword指的是在這種情況下acc調用此方法的對象,所以this.amount將修改該實例的amount

在您當前的代碼中,您只是修改了local variables

您還需要更新您的帳戶類有量和平衡屬性:

public class Account{ 
Scanner input = new Scanner (System.in); 
double balance = 0, amount = 0; 
+0

omg謝謝!這個關鍵字真的讓我困惑。 – Josh

+0

發現THIS關鍵字是什麼類?它說它找不到它 – Josh

+0

這個關鍵字不是任何類的一部分,它只是一個關鍵字來引用調用方法的實例。它應該工作,如果你只是這樣做。這是在每一個不像你所擁有的靜態方法中。 –

3

你有你的整個帳戶級設置,但沒有它裏面的實際餘額變量。將該變量從您的程序移到您的班級,它應該可以工作。經進一步看,你有那種混亂的比特兩者之間所以做到這一點:

public class MyProgram2{ 
    public static void main(String[] args){ 
    Scanner scan = new Scanner(System.in); 
    Scanner input = new Scanner (System.in); 
    String menu, outputString, poo; 
    int option = 1; 
    int id = 0; 
    Account acc = new Account(); 


    menu ="\n\t1 Create Account and ID" + 
     "\n\t2 Check balance" + 
     "\n\t3 Withdraw" + 
     "\n\t4 Deposit" + 
     "\n\t5 Get account ID" + 
     "\n\t6 Display Account Info" + 

     "\n\t0 Quit\n\n\n"; 

    do { 

     System.out.println(menu); 
     System.out.println("\tEnter your selection: "); 
     option = scan.nextInt(); 

     switch (option) { 

     case 1: //Create an account and set ID 
     System.out.print("Enter Your Account ID to create account:\t"); 
     id = input.nextInt(); 
     System.out.println("Account created!"); 
     break; 

     case 2:  //check balance 
     acc.checkBalance(); 
     break; 

     case 3:  //withdraw money 
     acc.withdraw(); 
     break; 

     case 4: //deposit money 
     acc.deposit(); 
     break; 

     case 5:  //get account id 
     acc.getID(id); 
     break; 

     case 7:  //display account info 
     System.out.print("option 7"); 
     break; 

     default: outputString = "\nInvalid Selection\n"; 
     System.out.println(outputString); 
     break; 

     } 
    } while (option != 0); 
    } 
} 

public class Account{ 

    Scanner scan = new Scanner(System.in); 
    Scanner input = new Scanner (System.in); double balance = 0; 

    public Account(){ 
    } 

    public void getID(id){ 
    System.out.println("Your account ID is:\t" + id); 
    } 

    public void checkBalance(){ 
    System.out.println("Your balance is:\t$" + balance); 
    } 

    public double withdraw(){ 
    System.out.println("How much do you want to withdraw?:\t$"); 
    double amount = input.nextDouble(); 
    balance -= amount; 
    } 

    public double deposit(){ 
    System.out.println("How much do you want to deposit?:\t"); 
    double amount = input.nextDouble(); 
    balance += amount; 
    return balance; 
    } 

    public void getAccountInfo(int id, double balance){ 

    } 
} 

這就是做這件事的一種方式,但就像我說的,你的設計有點類之間的交叉。您應該嘗試保留該類中某個對象的所有屬性,併爲該類創建函數以獲取/修改這些屬性。打印給用戶應該包含在負責給用戶界面的類中。

編輯:哎呀忘了,而在do while循環的結束......