我运行了这段代码(请参见下文),我希望它能提示用户输入第一个超级英雄的名字。但是,与其直接从第一行获取用户输入,不如直接转到“其他”之后的语句。这是控制台:
How many superheroes would you like?
3
Who is one of the superheroes?
Who is another superhero?
a
Who is another superhero?
b
Thank you
- 1.68
a - 2.78
b - 5.84
System.out.println("How many superheroes would you like?");
int n = input.nextInt();
String [] Super = new String[n];
for (int i = 0; i < n; i++) {
if (i == 0) {
System.out.println("Who is one of the superheroes?");
Super[i] = input.nextLine();
} else {
System.out.println("Who is another superhero?");
Super[i] = input.nextLine();
}
答案 0 :(得分:1)
尝试使用Super[i] = input.next();
代替input.nextLine()
正如@achAmháin所说,如果您输入的单词由空格分隔,则应使用:
System.out.println("Who is one of the superheroes?");
input.nextLine();
Super[i] = input.nextLine();
答案 1 :(得分:0)