循环不定数组

时间:2017-09-16 04:23:44

标签: java arrays loops java.util.scanner

我有这个代码,它运行得很好,但我没有使用代码中的名称和分数,而是希望能够输入我想要的所有名称和分数,直到没有输入名称(当&#34时;输入"被按下,没有输入任何内容。

Scoreboard highscores = new Scoreboard(3);

String[] names = { "Rob", "Mike", "Rose", "Robert", "Gudelio", "Anna", "Paul", "Bob" };
int[] scores = { 750, 1105, 590, 5690, 4003, 660, 720,600 };


for (int i = 0; i < names.length; i++) {
    GameEntry gE = new GameEntry(names[i], scores[i]);
    System.out.println("Adding " + gE);
    highscores.add(gE);
    System.out.println(" Scoreboard: " + highscores);
}
}

1 个答案:

答案 0 :(得分:-1)

Scanner input = new Scanner(System.in);

while(input.hasNext()) {
    // do what you need to do with the input here.
    // when the first empty string is encountered the loop will exit        
}