我想輸入對象數組內的值,但我似乎無法讓它工作。基本上,我只需要創建一個可以提取和存款的賬戶。我試着去找我的教授尋求幫助,她說我只需要初始化這些值,因爲它們最初都是空的。但是,我不確定這種語法,我的書有類似的方式,但不是確切的。任何人都可以幫助我嗎?將參數輸入到對象數組中?
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);
}
}
它的工作原理!謝謝你,以及關於--acct的事情(試圖爲用戶建立一個簡化的帳戶1-30,並暫時減去1以適應數組)。我剛剛意識到了這一邏輯..我一直持續到4試圖讓這個東西起作用,我最終重做了一百萬次代碼。只是一個問題:我認爲for循環創建了所有30個Account對象(對象引用),這是否意味着您首先需要創建數組引用(即30),然後單獨創建對象?我有一堆時間,謝謝你的幫助! – Submersed24