【发布时间】:2015-02-13 03:24:07
【问题描述】:
我想使用Scanner 的结果创建对象并将它们添加到数组中。
但是,每次我第二次要求用户输入时,它只会覆盖第一个对象。
如何将多个对象添加到数组中?
这是我的代码:
public void ajoutadd() {
int i=0;
boolean boucle=true;
while(i!=2){
Scanner thegame = new Scanner(System.in);
System.out.print("name: \n");
String jname = lejeu.nextLine();
System.out.print(jname);
Scanner qty = new Scanner(System.in);
System.out.print("qty \n");
int jqty = qty.nextInt();
Scanner cat = new Scanner(System.in);
System.out.print("cat: \n");
String categ = cat.nextLine();
Scanner price = new Scanner(System.in);
System.out.print("price: \n");
int jprice = price.nextInt();
Game agame = new Game(jname,jqty,categ,jprice);
System.out.print(unjeu.Nom);
// creating the array to contain the game(s)
ArrayList<Game> thegame = new ArrayList<Game>();
thegame.add(new Game(jname,jqty,categ,jprice));
// actually only display 1 object that is overwritten
// each time after the loop
System.out.println(thegame);
i=i++;
}
}
【问题讨论】:
标签: java arrays object methods java.util.scanner