数组输入保存在所有位置
Array inputs saves on all positions
我有一个代码,我试图将输入保存在数组中,但是当我这样做时,它保存了所有位置。
如果我的用户输入是 Klas、Philip 和 Comp,我得到的输出为:
来自 Comp 的 Klas Philip 添加了 100 号
来自 Comp 的 Klas Philip 添加了编号 101
来自 Comp 的 Klas Philip 添加了编号 102
来自 Comp 的 Klas Philip 添加了编号 103
但我确实希望将第一个用户输入保存为来自 Comp 的 Klas Philip,并添加编号 100,将第二个输入保存在编号 101 上,依此类推。
但是现在来自 Comp 的第一个输入 Klas Philip 保存了所有数字
这是代码
Class1[] all = new Class1[120];
int quantity = 0;
System.out.println("text: ");
String x = keyboard.nextLine();
System.out.println("text2: ");
String y = keyboard.nextLine();
System.out.println("text 3: ");
String z = keyboard.nextLine();
Class1 p = new Class1(x, y, z);
all[quantity++] = p;
for (int i = 100; i < all.length; i++)
System.out.println(p.getFirstName()+(" ")+p.getSurname()+" from " +p.getTeamName()+" with number " +(x) +" added");
if (quantity == all.length){
Class1[] temp = new Class1[all.length * 2];
for (int i = 0; i < all.length; i++)
temp[i] = all[i];
all = temp;
}
}
}
我发现很难弄清楚你想要实现什么。您可以编辑问题以使其更清晰吗?您将 Class1 数组的长度硬编码为 120,这就是为什么您得到重复的 print-outs.
改变
for (int i = 100; i < all.length; i++)
System.out.println(p.getFirstName()+(" ")+p.getSurname()+"
from " +p.getTeamName()+" with number " +(x) +" added");
到
for (int i = 100; i < all.length; i++)
System.out.println(p.getFirstName()+(" ")+p.getSurname()+"
from " +p.getTeamName()+" with number " +(i) +" added");
得到你描述的输出,即:
- 来自 Comp 的 Klas Philip 添加了 100
- 来自 Comp 的 Klas Philip 添加了编号 101
除非你的意思是你想要
- 来自 Comp 的 Klas Philip 添加了 100
- 来自 Gallifrey 的 Tom Baker 添加了 101
在这种情况下,我会先将每个用户输入存储在数组或列表中,然后循环遍历它以将其打印出来。例如:
Scanner keyboard = new Scanner(System.in);
List<Class1> all = new ArrayList<Class1>();
int quantity = 0;
while(quantity <2) // whatever the max number of entries should be
{
System.out.println("text: ");
String x = keyboard.nextLine();
System.out.println("text2: ");
String y = keyboard.nextLine();
System.out.println("text 3: ");
String z = keyboard.nextLine();
Class1 p = new Class1(x, y, z);
all.add(p);
quantity++;
}
for (int i = 0; i < all.size(); i++)
System.out.println(all.get(i).getFirstName()+(" ")+
all.get(i).getSurname()+" from " +all.get(i).getTeamName()+" with number
" +(i) +" added");
控制台输出:
text:
Jan
text2:
Klaas
text 3:
Huntelaar
text:
Tom
text2:
Baker
text 3:
Gallifrey
Jan Klaas from Huntelaar with number 0 added
Tom Baker from Gallifrey with number 1 added
我有一个代码,我试图将输入保存在数组中,但是当我这样做时,它保存了所有位置。 如果我的用户输入是 Klas、Philip 和 Comp,我得到的输出为:
来自 Comp 的 Klas Philip 添加了 100 号
来自 Comp 的 Klas Philip 添加了编号 101
来自 Comp 的 Klas Philip 添加了编号 102
来自 Comp 的 Klas Philip 添加了编号 103
但我确实希望将第一个用户输入保存为来自 Comp 的 Klas Philip,并添加编号 100,将第二个输入保存在编号 101 上,依此类推。 但是现在来自 Comp 的第一个输入 Klas Philip 保存了所有数字
这是代码
Class1[] all = new Class1[120];
int quantity = 0;
System.out.println("text: ");
String x = keyboard.nextLine();
System.out.println("text2: ");
String y = keyboard.nextLine();
System.out.println("text 3: ");
String z = keyboard.nextLine();
Class1 p = new Class1(x, y, z);
all[quantity++] = p;
for (int i = 100; i < all.length; i++)
System.out.println(p.getFirstName()+(" ")+p.getSurname()+" from " +p.getTeamName()+" with number " +(x) +" added");
if (quantity == all.length){
Class1[] temp = new Class1[all.length * 2];
for (int i = 0; i < all.length; i++)
temp[i] = all[i];
all = temp;
}
}
}
我发现很难弄清楚你想要实现什么。您可以编辑问题以使其更清晰吗?您将 Class1 数组的长度硬编码为 120,这就是为什么您得到重复的 print-outs.
改变
for (int i = 100; i < all.length; i++)
System.out.println(p.getFirstName()+(" ")+p.getSurname()+"
from " +p.getTeamName()+" with number " +(x) +" added");
到
for (int i = 100; i < all.length; i++)
System.out.println(p.getFirstName()+(" ")+p.getSurname()+"
from " +p.getTeamName()+" with number " +(i) +" added");
得到你描述的输出,即:
- 来自 Comp 的 Klas Philip 添加了 100
- 来自 Comp 的 Klas Philip 添加了编号 101
除非你的意思是你想要
- 来自 Comp 的 Klas Philip 添加了 100
- 来自 Gallifrey 的 Tom Baker 添加了 101
在这种情况下,我会先将每个用户输入存储在数组或列表中,然后循环遍历它以将其打印出来。例如:
Scanner keyboard = new Scanner(System.in);
List<Class1> all = new ArrayList<Class1>();
int quantity = 0;
while(quantity <2) // whatever the max number of entries should be
{
System.out.println("text: ");
String x = keyboard.nextLine();
System.out.println("text2: ");
String y = keyboard.nextLine();
System.out.println("text 3: ");
String z = keyboard.nextLine();
Class1 p = new Class1(x, y, z);
all.add(p);
quantity++;
}
for (int i = 0; i < all.size(); i++)
System.out.println(all.get(i).getFirstName()+(" ")+
all.get(i).getSurname()+" from " +all.get(i).getTeamName()+" with number
" +(i) +" added");
控制台输出:
text:
Jan
text2:
Klaas
text 3:
Huntelaar
text:
Tom
text2:
Baker
text 3:
Gallifrey
Jan Klaas from Huntelaar with number 0 added
Tom Baker from Gallifrey with number 1 added