我想在for循环中从一个类创建几个对象。但我不知道如何编码。我写的内容创建了一个新对象,但它覆盖了前一个对象。在Java 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
@ msc87如果您将帮助解决问题的答案标记为已接受的答案(加上您获得2个业力),这很有帮助! – jbranchaud