我正在使用扫描仪用数据填充数组。它似乎有效,因为当我在“if”语句中打印 (player[i].Name) 时,它会打印一个值。但是,当我尝试在语句 (players[1].Name) 之外打印此值时,它会给出一个空值。
int i = 0;
while (scanner.hasNextLine()) {
if (scanner.hasNext()) {
players[i].Name = scanner.next();
System.out.println(players[i].Name);
}
if (scanner.hasNext()) {
players[i].Position = scanner.next();
System.out.println(players[i].Position);
}
if (scanner.hasNext()) {
players[i].Height = scanner.next();
System.out.println(players[i].Height);
}
if (scanner.hasNext()) {
players[i].Weight = scanner.next();
System.out.println(players[i].Weight);
}
i++;
}//end while