我试图在一个对象数组中输入值,但我似乎无法让它工作。从本质上讲,我只需要创建一系列可以提取和存款的帐户。我试着去找我的教授寻求帮助,她说我只需要初始化值,因为它们最初都是null。但是,我不确定这个的语法,我的书有类似的方法,但不完全一样。有人可以帮我这个吗?
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);
}
}
答案 0 :(得分: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() + "$");
无论如何,这有点难以辨别。代码有几个问题,通过问题答案调试不太实用。
答案 1 :(得分:2)
这一行
acct[--num].withdraw(WithdrawMoney);
有问题。如果用户输入数字 0 怎么办?然后num
将等于 0 ,&amp;你会得到一个ArrayIndexOutOfBoundsException
。 (除了 @Janos 所说的)
由于您从 1 开始初始化帐号,您应该检查用户是否输入 0 ,&amp;相应地采取行动。