Java - 扫描器导致循环到 运行 两次
Java - Scanner causes loop to run twice
我对 Java 和一般编程还很陌生。我正在尝试询问名人的名字并将它们放在一个数组中。当我尝试使用扫描仪读取用户的输入时,它设法存储了名称,但在循环条件之前运行了 System.out.println("Name is too short, try again");
行。
以下是我的代码中的相关部分:
public static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
String celeb[];
int number;
System.out.println("Welcome to Celebrity Guesser!");
System.out.println("How many celebrities do you want to name?");
number = scan.nextInt();
celeb = new String[number];
for(int i = 0; i < number; i++) { //asks for user input
celeb[i] = celebInput(i + 1);
}
for(int i = 0; i < number; i++) { //asks questions to user
String tempCeleb = celeb[i];
celebOutput(tempCeleb, i + 1);
}
}
//Reads names of celebrities from the user
static String celebInput(int n) {
System.out.println("\nType in celebrity #" + n);
String c = scan.nextLine();
while(c.length() < 6) {
System.out.println("Name is too short, try again");
c = scan.nextLine();
}
return c;
}
这是输出:
Welcome to Celebrity Guesser!
How many celebrities do you want to name?
2
Type in celebrity #1
Name is too short, try again
Miley Cyrus
Type in celebrity #2
Katy Perry
为什么这个问题只发生在 "celebrity #1" 而不是 "celebrity #2"?谢谢。
scan.nextInt 只会从输入流中删除“2”。你回车时输入的“\n”(换行符)还在输入流中;因此,第一个 nextLine 调用将 return 一个空行。
澄清一下:
// assuming we enter "2\ntest\n" at the command line
Scanner s = new Scanner(System.in); // s now contains "2\ntest\n"
System.out.println(s.nextInt()); // s now contains "\ntest"
System.out.println(s.nextLine()); // s now contains "test"
System.out.println(s.nextLine()); // s is empty
输出:
"2"
""
"test"
解决办法是在nextInt之后调用nextLine清除空行。
尝试 number = Integer.parseInt(scan.nextLine());
而不是 number = scan.nextInt();
希望有用。让我知道。
我对 Java 和一般编程还很陌生。我正在尝试询问名人的名字并将它们放在一个数组中。当我尝试使用扫描仪读取用户的输入时,它设法存储了名称,但在循环条件之前运行了 System.out.println("Name is too short, try again");
行。
以下是我的代码中的相关部分:
public static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
String celeb[];
int number;
System.out.println("Welcome to Celebrity Guesser!");
System.out.println("How many celebrities do you want to name?");
number = scan.nextInt();
celeb = new String[number];
for(int i = 0; i < number; i++) { //asks for user input
celeb[i] = celebInput(i + 1);
}
for(int i = 0; i < number; i++) { //asks questions to user
String tempCeleb = celeb[i];
celebOutput(tempCeleb, i + 1);
}
}
//Reads names of celebrities from the user
static String celebInput(int n) {
System.out.println("\nType in celebrity #" + n);
String c = scan.nextLine();
while(c.length() < 6) {
System.out.println("Name is too short, try again");
c = scan.nextLine();
}
return c;
}
这是输出:
Welcome to Celebrity Guesser!
How many celebrities do you want to name?
2
Type in celebrity #1
Name is too short, try again
Miley Cyrus
Type in celebrity #2
Katy Perry
为什么这个问题只发生在 "celebrity #1" 而不是 "celebrity #2"?谢谢。
scan.nextInt 只会从输入流中删除“2”。你回车时输入的“\n”(换行符)还在输入流中;因此,第一个 nextLine 调用将 return 一个空行。
澄清一下:
// assuming we enter "2\ntest\n" at the command line
Scanner s = new Scanner(System.in); // s now contains "2\ntest\n"
System.out.println(s.nextInt()); // s now contains "\ntest"
System.out.println(s.nextLine()); // s now contains "test"
System.out.println(s.nextLine()); // s is empty
输出:
"2"
""
"test"
解决办法是在nextInt之后调用nextLine清除空行。
尝试 number = Integer.parseInt(scan.nextLine());
而不是 number = scan.nextInt();
希望有用。让我知道。