坚持通过 for 循环创建对象
Posted
技术标签:
【中文标题】坚持通过 for 循环创建对象【英文标题】:Stuck on creating a object via for loop 【发布时间】:2014-11-27 17:53:03 【问题描述】:我一直在为帐户创建对象,我不知道问题出在哪里。我的输出仅显示最近输入的值。我想查看我创建的打印详细信息(尤其是不同的 id),但我不知道该怎么做。
请检查我的代码,因为我认为我的循环中存在错误,在某种程度上显示了不需要显示的显示消息。
BankSystem.java:
import java.io.*;
import java.util.Scanner;
public class BankSystem
public static void main(String[] args) throws IOException
Scanner in = new Scanner(System.in);
BankAccount account[] = new BankAccount[5];
BankClient client[] = new BankClient[5];
BankSystem myBankSystem = new BankSystem();
String MainMenu;
do
System.out.println("");
System.out.println("------PLP BANK SYSTEM------");
System.out.println("Main Menu: ");
System.out.println("[A]ccount Management" );
System.out.println("[C]lient Management" );
System.out.println("[Q]uit" );
System.out.println("");
System.out.print("Please select letter: ");
MainMenu = in.nextLine();
switch (MainMenu.toLowerCase())
case "a":
System.out.println("");
System.out.println("------PLP BANK SYSTEM------");
System.out.println("Account Management:" );
System.out.println("[N]ew Account" );
System.out.println("[A]Apply Interest to All Accounts" );
System.out.println("[L]ist All Accounts" );
System.out.println("[F]ind an Account" );
System.out.println("[D]eposit to an Account" );
System.out.println("[W]ithdraw from an Account" );
System.out.println("[R]eturn to Main Menu" );
System.out.println("");
System.out.print("Please select letter: " );
String AccountManagement = in.nextLine();
switch (AccountManagement.toLowerCase())
case "n": //NEW ACCOUNT
System.out.print("Please input your desired ID number: " );
int id = in.nextInt();
System.out.print("Please input your desired Balance: " );
double balance = in.nextDouble();
System.out.print("Please input your desired Interest Rate: " );
double interestRate = in.nextDouble();
// IS THE PROBLEM IS HERE ON MY LOOP?
for (int i=0; i<account.length; i++)
account[i] = new BankAccount(id, balance, interestRate);
break;
case "a": //APPLY INTEREST TO ALL ACCOUNTS
System.out.println("For all accounts, compute and compound:" );
System.out.println("[M]onthly" );
System.out.println("[Q]uarterly" );
System.out.println("[A]nnually");
System.out.println("[C]ancel");
break;
case "l": //LIST ID NUMBERS OF ALL ACCOUNTS
System.out.println("List of all Accounts: ");
account[1].printDetails(); // OR THE PROBLEM IS HERE?
account[2].printDetails();
break;
case "f": //FIND ACCOUNT
System.out.println("Enter ID number: " );
break;
case "d": //DEPOSIT
System.out.println("Enter ID number: " );
System.out.println("Enter Deposit amount: " );
break;
case "w": //WITHDRAW
System.out.println("Enter ID number: " );
System.out.println("Enter Withdraw amount: " );
break;
case "r": // RETURN
break;
case "c":
System.out.println("");
System.out.println("------PLP BANK SYSTEM------");
System.out.println("Client Management:");
System.out.println("[N]ew Client" );
System.out.println("[L]List All Clients" );
System.out.println("[F]ind a Client" );
System.out.println("[R]eturn to Main Menu" );
System.out.println("");
System.out.print("Please select letter: " );
String ClientManagement = in.nextLine();
switch (ClientManagement.toLowerCase())
case "n": //NEW CLIENT
System.out.println("Enter ID number: " );
System.out.println("Please input your Name: " );
System.out.println("Please input your account ID number: " );
break;
case "l": //LIST ALL CLIENT
break;
case "f": //FIND A CLIENT
System.out.println("Enter ID number: " );
break;
case "r":
break;
default:
System.out.println("Invalid entry, Please try again!");
break;
default:
System.out.println("Invalid entry, Please try again!");
while (!MainMenu.equals("q"));
System.out.println("Thank you for using my program!");
BankAccount.java:
public class BankAccount
private double balance;
private double interestRate;
private int id;
public BankAccount (int id, double initialDeposit, double initialIntRate)
//Constructor
this.id=id;
this.balance=initialDeposit;
this.interestRate=initialIntRate;
public double getBalance()
return balance;
public double getInterestRate()
return interestRate;
public int getIDNumber()
return id;
public void printDetails()
System.out.println("ID Number is: " +id );
System.out.println("Current balance is: "+balance );
System.out.println("Interest rate is: "+interestRate+"%");
public double computeMonthlyInterest()
interestRate=balance*interestRate;
return interestRate;
public void applyMonthlyInterest()
this.balance=balance+interestRate;
public void applyQuarterlyInterest()
this.balance=balance+(interestRate*3);
public void applyAnnualInterest()
this.balance=balance+(interestRate*12);
public void deposit(double amount)
this.balance += amount;
public boolean withdraw (double amount)
if (amount>balance)
System.out.println("Withdraw amount is more than balance, Please try again.");
return false;
else
this.balance -= amount;
return true;
【问题讨论】:
请缩进您的代码。目前几乎无法阅读。请更准确地描述您的问题(期望的行为是什么等) 你一个switch
structur 在case
语句之间没有break;
并且有一个while
缺少一个主体(在第一个代码sn-p 中)
缩小问题范围。当然,大部分代码与问题无关。正如其他人所说,请花时间格式化和缩进您的代码。
我的错,对不起,编辑完成谢谢。我的问题是当我创建一个新帐户时。我只是替换它。
@RaycharlesDelaCruz 不要只是发表评论告诉我们Jamie 的解决方案对您有帮助,而是接受他的回答
【参考方案1】:
你真的应该考虑把你的代码分解成子程序;将其分解将有助于划分不同的功能。
关于你的错误;我认为您是在说所有帐户都具有相同的价值,而您并不期望如此。
您的代码如下,创建新账户时:
for (int i=0; i<account.length; i++)
account[i] = new BankAccount(id, balance, interestRate);
每次运行该行时,都会将数组中的每个元素替换为新的 BankAccount 对象。
【讨论】:
我应该替换什么代码?我想创建不同的帐户,而不是替换几乎花了 3 个小时搜索这个 感谢杰米的启蒙。我现在知道该怎么做了 您还应该考虑将您的银行账户对象放入一个列表中,而不是一个固定大小的数组。查看 javadoc 以了解 ArrayList 或 HashMap 等集合。【参考方案2】:您的问题是,您使用最新输入的银行帐户填写了整个银行帐户。
for (int i=0; i<account.length; i++)
account[i] = new BankAccount(id, balance, interestRate);
相反,在收到银行账户信息后,您应该只创建一个新的 BankAccount,并且数组的索引应该是当前银行账户数量 +1。
所以在 do 语句之前创建一个变量来存储银行账户的当前金额。
int amountOfBankAccounts = 0;
并在for的位置使用它并删除for
if(amountOfBankAccounts < 5)
account[amountOfBankAccounts++] = new BankAccount(id, balance, interestRate);
else
System.out.println("Can not create a new account anymore, array is full!");
这应该可以解决它。一旦可行,我建议您花一些时间创建函数,以使您的代码更具可读性和可重用性。
【讨论】:
如何标记 juru 先生。对不起,我只是新来的。我可以要求最后一件事吗?用于查找帐号的 if 语句怎么样。 只需按答案左上角的 V 形标志。我真的不知道你想找到什么。但是,如果您想查找具有特定 ID 的 BankAccount,您可以使用循环遍历所有银行账户并使用此 if 语句 if(givenId == bankAccounts[i].getIDNumber())以上是关于坚持通过 for 循环创建对象的主要内容,如果未能解决你的问题,请参考以下文章