在Java中,我尝试过
try (Scanner sc = new Scanner(System.in)) {
while (sc.hasNextLine()) {
System.out.print("Name: ");
String name = sc.nextLine();
System.out.println("Name is \"" + name + "\"");
}
}
Name:
但在要求输入之前它不会输出。
控制台只显示一个空的控制台窗口,我可以在其中输入名称。
Name:
在询问名称之前如何确保已输出?
编辑
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Name: ");
while (sc.hasNextLine()) {
String name = sc.nextLine();
System.out.println("Name is \"" + name + "\"");
System.out.print("Age: ");
int age = sc.nextInt();
System.out.println("Age is " + age);
System.out.print("Name: ");
}