【发布时间】:2016-12-13 02:10:47
【问题描述】:
如果问题没有意义,我深表歉意。我制作了一个管理银行账户的银行账户程序,我接到了一项任务,要求我设计一种方法,为所有银行账户增加 3% 的利息。
这是 Account 类的第一部分:
public class Accounts
{
private NumberFormat fmt = NumberFormat.getCurrencyInstance();
private final double RATE = 0.03; // interest rate of 3.5%
private int acctNumber;
private double balance;
private String name;
//public Accounts[] account;
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Accounts (String owner, int account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
单个账户加息方法如下:
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
这是我正在研究的让多个银行账户获得利息的方法:
public double addInterestAll ()
for (int i = 0; i <= 29; i++){
account[i].addInterestAll();
}
return balance;
}
上面的account变量是我在main方法中做的一个数组,但是因为我在main方法中声明了这个数组,编译器找不到。我不确定如何使用另一种方法来完成这项工作。
这里是主要方法:
public class SixSix
{
public static void main (String []args)
{
Accounts[] account = new Accounts[30];
account[0] = new Accounts ("Gerry Agam", 1, 1111.00);
account[1] = new Accounts ("Jules Innes", 2, 2222.00);
account[2] = new Accounts ("Shani Cahya", 3, 3333.00);
account[3] = new Accounts ("Bao Shelly", 4, 4444.00);
account[4] = new Accounts ("Sasha Ashley", 5, 5555.00);
account[5] = new Accounts ("Özgür Mor", 6, 6666.00);
account[6] = new Accounts ("Katlego Lashawn", 7, 7777.00);
account[7] = new Accounts ("An Narcisse", 8, 8888.00);
account[8] = new Accounts ("Amal Lalita", 9, 9999.00);
account[9] = new Accounts ("Sabah Dalitso", 10, 1010.00);
account[10] = new Accounts ("Charley Robin", 11, 1111.00);
account[11] = new Accounts ("Jo Oluwasegun", 12, 1212.00);
account[12] = new Accounts ("Vic Nithya", 13, 1313.00);
account[13] = new Accounts ("Nuka Kirabo", 14, 1414.00);
account[14] = new Accounts ("Madhu Munashe", 15, 1515.00);
account[15] = new Accounts ("Saša Guanting", 16, 1717.00);
account[16] = new Accounts ("Esmé Emem", 17, 1717.00);
account[17] = new Accounts ("Ren Thoko", 18, 1818.00);
account[18] = new Accounts ("Lei Yaroslava", 19, 1919.00);
account[19] = new Accounts ("Xuân Seong-Hy", 20, 2020.00);
account[20] = new Accounts ("Tu Maria", 21, 2121.00);
account[21] = new Accounts ("Nasim Noor", 22, 2222.00);
account[22] = new Accounts ("Chandra Morgan", 23, 2323.00);
account[23] = new Accounts ("Camille Jie", 24, 2525.00);
account[24] = new Accounts ("Adetokunbo Sam", 25, 2626.00);
account[25] = new Accounts ("Makena Sothy", 26, 2726.00);
account[26] = new Accounts ("Hanne Kit", 27, 2727.00);
account[27] = new Accounts ("Almas Florence", 28, 2828.00);
account[28] = new Accounts ("Yoshi Saša", 29, 2929.00);
account[29] = new Accounts ("Cahyo Ime", 30, 3030.00);
for (int i = 0; i <=29; i++)
System.out.println(account[i]);
System.out.println("Depositing $100.01 from every account and withdrawing $50.05 out of every account with fee");
for (int i = 0; i <=29; i++){
account[i].deposit (100.01);
account[i].withdraw (50.05, 0.0);
}
System.out.println("After deposit and withdrawl");
for (int i = 0; i <=29; i++)
System.out.println(account[i]);
System.out.println("Account number 30 will make invalid transactions...");
account[29].deposit (-1000.00);
account[29].withdraw (9999999999.00, 0.1);
account[29].toString();
System.out.println("Adding 3 percent interest to all accounts.");
//account.addInterestAll(account);
}
}
总之,我不确定如何为数组中的每个帐户添加兴趣。提前谢谢你
【问题讨论】:
-
将数组传递给迭代和更新的方法。将讨论是否应该返回一个 new 数组,这是一个单独的问题。您在第一个简单的选项时遇到了什么具体问题?
-
您的代码中似乎有正确的想法:最后一行是
account.addInterestAll(account);,您已将其注释掉,原因不明。这正是您想要的 - 您只需更新您的方法以接受account作为参数。 -
@Zexus 如何让方法接受
account作为参数? -
将方法声明为
public double addInterestAll(Accounts[] account)
标签: java arrays class for-loop methods