如何在扫描仪对象后添加一个空行?
How do I add a blank line after the scanner object?
我是 Java 的新手,我在让我的代码执行我想要的操作时遇到了一些问题。
所以这是我的代码:
System.out.println("Please enter an integer: ");
int another_int_value = Integer.parseInt(input.nextLine());
这会输出消息 Please enter an integer:
,然后是一个空行,用户可以在其中键入一个整数,假设用户输入了整数 3
。
以下行紧接在 3
之后开始,但我希望它跳过一行,我该如何实现?
我尝试添加 input.nextLine()
行,但随后用户必须按 ENTER 两次,我不希望这样。有什么建议么?
或者,您可以通过删除 .println 并仅使用 .print 将其设置到用户在分号后输入代码的位置。之后不需要额外的代码。
System.out.print("Please enter an integer: ");
int another_int_value = Integer.parseInt(input.nextLine());
这应该有效
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter an integer: ");
System.out.println();
int another_int_value = Integer.parseInt(input.nextLine());
System.out.println();
}
}
我是 Java 的新手,我在让我的代码执行我想要的操作时遇到了一些问题。 所以这是我的代码:
System.out.println("Please enter an integer: ");
int another_int_value = Integer.parseInt(input.nextLine());
这会输出消息 Please enter an integer:
,然后是一个空行,用户可以在其中键入一个整数,假设用户输入了整数 3
。
以下行紧接在 3
之后开始,但我希望它跳过一行,我该如何实现?
我尝试添加 input.nextLine()
行,但随后用户必须按 ENTER 两次,我不希望这样。有什么建议么?
或者,您可以通过删除 .println 并仅使用 .print 将其设置到用户在分号后输入代码的位置。之后不需要额外的代码。
System.out.print("Please enter an integer: ");
int another_int_value = Integer.parseInt(input.nextLine());
这应该有效
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter an integer: ");
System.out.println();
int another_int_value = Integer.parseInt(input.nextLine());
System.out.println();
}
}