2015-12-14 69 views
1

我想輸入對象數組內的值,但我似乎無法讓它工作。基本上,我只需要創建一個可以提取和存款的賬戶。我試着去找我的教授尋求幫助,她說我只需要初始化這些值,因爲它們最初都是空的。但是,我不確定這種語法,我的書有類似的方式,但不是確切的。任何人都可以幫助我嗎?將參數輸入到對象數組中?

import java.util.Scanner; 
import java.text.NumberFormat; 
public class AccountMain 
{ 
    public static void main(String[] args) 
    { 
    Scanner scan = new Scanner(System.in); 
    Account[] acct = new Account[30]; 
    for (int count = 0; count < 30; count++) 
    { 
     acct[count] = new Account("", 0, 0); 
    } 
                       //creates accounts 
    acct[0].Account("Jerry Longhorn", 1, 50.50); 
    acct[1].Account("Jimmy Longhorn", 2, 700.20); 
    acct[2].Account("Jenny Longhorn", 3, 10000.50); 
    acct[3].Account("Jeffrey Longhorn", 4, 400.20); 
    acct[4].Account("Jack Longhorn", 5, 500.20); 

                       //Determines withdraw or deposit 
    System.out.println("Please enter your account number: "); 
    int num = scan.nextInt(); 
    System.out.println("Enter W for withdrawl; D for deposit: "); 
    char choice = scan.next().charAt(0); 

    if (choice == 'W' || choice == 'w' || choice == 'D' || choice == 'd') 
    { 

     if (choice == 'W' || choice == 'w') 
     { 
     System.out.println("Enter amount to withdraw: "); 
     double WithdrawMoney = scan.nextDouble(); 
     acct[--num].withdraw(WithdrawMoney);         //MUST --num in order to account for 0 
     System.out.println("User # " + num + " funds after withdraw: " + acct[--num].getBalance() + "$"); 
     System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$"); 
     } 
     if (choice == 'D' || choice == 'd') 
     { 
     System.out.println("Enter amount to deposit: "); 
     double DepositMoney = scan.nextDouble(); 
     acct[--num].deposite(DepositMoney);         //MUST --num in order to account for 0 
     System.out.println("User # " + num + " funds after deposit: " + acct[--num].getBalance() + "$"); 
     System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$"); 
     } 
     else 
     System.out.println("Invalid input."); 



    } 
    } 
} 

支持類:

import java.text.NumberFormat; //links to Part2 

public class Account 
{ 
    private final double RATE = 0.03; //Interest is 3% 

    private int acctNumber; 
    private String name; 
    private double balance; 

    //Defines owner, account number, and initial balance. 
    public Account(String owner, int account, double initial) 
    { 
    name = owner; 
    acctNumber = account; 
    balance = initial; 
    } 

    //deposits a specified amount and returns new balance 
    public double deposit(double amount) 
    { 
    balance = balance + amount; 
    return balance; 
    } 

    //withdraws the specified amount from the account and applies the fee 
    //             + returns balance 
    public double withdraw(double amount) 
    { 
    int fee = 1; 
    balance = balance - amount - fee; 
    return balance; 
    } 

    //Adds interest to the account 
    public double addInterest() 
    { 
    balance += (balance * RATE); 
    return balance; 
    } 
    public double getBalance() 
    { 
    return balance; 
    } 

    //returns a one line description of the account as a string 
    public String toString() 
    { 
    NumberFormat fmt = NumberFormat.getCurrencyInstance(); 
    return acctNumber + "/t" + name + "/t" + fmt.format(balance); 
    } 
} 

回答

5

這是無效的語法:

acct[0].Account("Jerry Longhorn", 1, 50.50); 
acct[1].Account("Jimmy Longhorn", 2, 700.20); 
acct[2].Account("Jenny Longhorn", 3, 10000.50); 
acct[3].Account("Jeffrey Longhorn", 4, 400.20); 
acct[4].Account("Jack Longhorn", 5, 500.20); 

你可能是指這個:

acct[0] = new Account("Jerry Longhorn", 1, 50.50); 
acct[1] = new Account("Jimmy Longhorn", 2, 700.20); 
acct[2] = new Account("Jenny Longhorn", 3, 10000.50); 
acct[3] = new Account("Jeffrey Longhorn", 4, 400.20); 
acct[4] = new Account("Jack Longhorn", 5, 500.20); 

順便說一句,你也有一個語法錯誤的位置:

acct[--num].deposite(DepositMoney);          

看起來像一個簡單的拼寫錯誤:

acct[--num].deposit(DepositMoney);          

還有......還有很多問題....例如:

acct[--num].withdraw(WithdrawMoney);         //MUST --num in order to account for 0 
System.out.println("User # " + num + " funds after withdraw: " + acct[--num].getBalance() + "$"); 
System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$"); 

這裏是怎麼回事--num重複索引? 我真的懷疑你真的想減少那個指數變量。 我懷疑這是更接近你其實是想(num,而不是所有的--num):

acct[num].withdraw(WithdrawMoney);         //MUST --num in order to account for 0 
System.out.println("User # " + num + " funds after withdraw: " + acct[num].getBalance() + "$"); 
System.out.println("User # " + num + " funds after interest: " + acct[num].addInterest() + "$"); 

在任何情況下,這是一個有點難以啓齒。代碼有幾個問題,通過問題 - 答案進行調試並不實用。

+0

它的工作原理!謝謝你,以及關於--acct的事情(試圖爲用戶建立一個簡化的帳戶1-30,並暫時減去1以適應數組)。我剛剛意識到了這一邏輯..我一直持續到4試圖讓這個東西起作用,我最終重做了一百萬次代碼。只是一個問題:我認爲for循環創建了所有30個Account對象(對象引用),這是否意味着您首先需要創建數組引用(即30),然後單獨創建對象?我有一堆時間,謝謝你的幫助! – Submersed24

2

此行

acct[--num].withdraw(WithdrawMoney);  

是有問題的。如果用戶輸入的號碼是?然後num將等於,&您將得到一個ArrayIndexOutOfBoundsException。 (除了什麼@Janos說)

既然你初始化賬號出發的,你應該做一個檢查,看看如果用戶輸入相應的0 ,&行爲。