为什么这会跳过第一行?

时间:2017-12-11 11:25:38

标签: java

当我运行此代码时,它会跳过第一行(“输入名字:”)并询问姓氏。我该如何解决这个问题?

public static void newProduct() {
    System.out.println("Input first name: ");
    String fname = scan.nextLine();
    System.out.println("Input last name: ");
    String lname = scan.nextLine();
}

每次运行时的输出是:

Input first name: <User cannot input as this line gets skipped>
Input last name: <User can input>

提前致谢。

1 个答案:

答案 0 :(得分:-1)

试试这个

public class Readname {


public static void main(String[] args) {


            Scanner scan = new Scanner(System.in);

            System.out.print("Give a First Name :");
            String text = scan.nextLine();
            System.out.print("Give a Last Name :");
            String text2 = scan.nextLine();
            System.out.println("Full Name: "+text+" "+text2);//to bond the last and first name 

}

}