在初始循环后,循环打印输出两次

时间:2016-04-19 02:46:30

标签: java

**删除了代码以防止复制,因为这是一项正在进行的分配

问题 - 循环打印输出两次

1 个答案:

答案 0 :(得分:4)

问题在于s.nextInt()命令它只读取int值。因此,当您继续阅读s.nextLine()时,您会得到" \ n"回车键。因此,要跳过此处,您必须添加s.nextLine()。希望现在应该清楚这一点。

尝试类似的东西:

System.out.print("Insert a number: ");
int number = input.nextInt();

input.nextLine(); // This line you have to add (It consumes the \n character)

System.out.print("Text1: ");
String text1 = input.nextLine();

System.out.print("Text2: ");
String text2 = input.nextLine();