【发布时间】:2014-09-26 02:38:36
【问题描述】:
我的老师向我们展示了如何使用 ArrayList 来存储信息,但我遇到了麻烦。我不明白的是如何引用我存储在数组列表中的“帐户”,然后对其应用方法。例如,我正在尝试对存储在 ArrayList 中的帐户使用“存款”方法。
import java.util.Scanner;
import java.util.ArrayList;
public class Engine
{
public static void Engine()
{
ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
Scanner Reader = new Scanner(System.in);
BankAccount n = new BankAccount();
String response = Reader.nextLine();
boolean keepGoing = true;
while(keepGoing)
{
System.out.println("Welcome to The Bank of Money, what would you like to do?\n enter code hereenter n to create a new account, enter e to use an existing account, or enter q to quit to main menu");
response = Reader.nextLine();
if(response.equals("q")) keepGoing = false;
if(response.equals("n")) accounts.add(new BankAccount());
if(response.equals("e"))System.out.println("what is your account number?");
String accountNum = Reader.nextLine();
}
System.out.println("press 1 to deposit money");
System.out.println("press 2 to withdraw money");
System.out.println("press 3 to check your account balance");
System.out.println("press 4 to check your account's interest");
System.out.println("press 5 to quit");
String response2 = Reader.nextLine();
if (Reader.nextLine().equals("1"))
{
for(int i = 0; i<accounts.size();i++)
{
if (accounts.get(i).equals(accountNum))
{
accounts.get(i).deposit(amount);
}
}
}
}
}
我更新的代码
【问题讨论】:
-
我希望我提供了足够的信息,任何帮助将不胜感激
-
银行账户数组列表,然后呢?我们不需要你老师教的东西?
-
你将如何检查用户输入?我想 switch 语句将是更好的选择,如果也是。不是吗?
-
如果您在 BankAccount 类中有一个 id 字段,则遍历每个对象的数组列表,使用 getter 获取 id 并将其与输入 id 进行比较(如果等于执行操作)
标签: java arraylist nested-loops