【发布时间】:2012-05-10 22:04:38
【问题描述】:
我想在 for 循环中从一个类创建多个对象。但我不知道如何编码。我所写的内容创建了一个新对象,但它覆盖了之前的对象。
package assginment1_version4;
import java.util.*;
public class Client {
public static void main (String[] args) {
System.out.println ("this is a bill database");
System.out.println ("add a user?(Y/N)");
Scanner input = new Scanner(System.in);
String answer = input.nextLine ();
ArrayList ary = new ArrayList ();
for (int i=1 ; i < 100; i++) {
if (answer.equalsIgnoreCase("y")) {
Bill bill1 = new Bill();
System.out.println("user first name:");
bill1.setFname (input.nextLine());
System.out.println("user Last name:");
bill1.setLname (input.nextLine());
System.out.println ("add a user?(Y/N)");
answer = input.nextLine ();
} else if (answer.equalsIgnoreCase ("n")) {
if (Bill.getBillCounter () == 0) {
System.out.println ("the Database is empty");
break;
} else {
System.out.println ("Number of Users: "
+ Bill.getBillCounter ());
break;
}
} else {
while (!answer.equalsIgnoreCase ("n")
&& !answer.equalsIgnoreCase ("y")) {
System.out.println ("add a user?(Y/N)");
answer = input.nextLine ();
}
}
}
}
}
请帮我完成这段代码。
【问题讨论】:
-
你到底想做什么?
-
我想向这个数据库添加新对象(bill2、bill3、...),但是我的代码将新对象写入了前一个对象。我想将所有对象信息保留在我的数据库中。
-
@msc87 如果您将有助于解决问题的答案标记为已接受的答案(加上您获得 2 业力),这将很有帮助!
标签: java