我想使用扫描仪的结果创建对象并将其添加到数组中.
但是,每次第二次要求用户输入时,它只会覆盖第一个对象.
如何将多个对象添加到数组?
这是我的代码:
public void ajoutadd() {
int i=0;
boolean boucle=true;
while(i!=2){
Scanner thegame = new Scanner(System.in);
System.out.print("name:
");
String jname = lejeu.nextLine();
System.out.print(jname);
Scanner qty = new Scanner(System.in);
System.out.print("qty
");
int jqty = qty.nextInt();
Scanner cat = new Scanner(System.in);
System.out.print("cat:
");
String categ = cat.nextLine();
Scanner price = new Scanner(System.in);
System.out.print("price:
");
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 thegame = new ArrayList();
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++;
}
}