System.out.print()无法处理我的代码,还是它是我的编译器



我正在使用goesPache NetBeans作为我的编译器,这就是我的代码

package com.mycompany.mavenproject4;
import java.util.*;
public class MyZodiac {
public static void main(String[] args) {
Scanner zodiac = new Scanner(System.in);     

System.out.println("Chinese Zodiac Sign");
System.out.print("Enter your Name: ");
System.out.print("");
String name = zodiac.next();
zodiac.nextLine();

System.out.print("Enter your year of birth: ");
int year = zodiac.nextInt(); 
}}

然后当我运行这个代码时,它变成了这样的

Chinese Zodiac Sign
<user input name>
<user input year>
Enter your Name: Enter your year of birth:

我想要的是

Chinese Zodiac Sign
Enter your name: <user input>
Enter your year of birth:<user input>

System.out.println包含一个换行符,它会导致缓冲输出的隐式刷新(如System.out(。您可以显式flush。打印""没有任何作用。简而言之,更改

System.out.print("Enter your Name: ");
System.out.print("");
String name = zodiac.next();
zodiac.nextLine();
System.out.print("Enter your year of birth: ");

System.out.print("Enter your Name: ");
System.out.flush();
String name = zodiac.next();
zodiac.nextLine();
System.out.print("Enter your year of birth: ");
System.out.flush();

最新更新