Java跳过一行代码?非常基本的java

时间:2013-10-27 22:37:58

标签: java

如果你看一下代码行

System.out.println("Please enter the firstname of your favourite female author");
mFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
mSurname = scanner.nextLine();

它完全跳过名字部分并直接姓氏?任何想法为什么会发生这种情况?

import java.util.*;
    'class university{
        public static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            Person2 mPerson, fPerson;

        String fFirstName, fSurname, mFirstName, mSurname;
        int fAge, mAge;

        System.out.println("Please enter the firstname of your favourite female author");
        fFirstName = scanner.nextLine();
        System.out.println("Please enter her second name");
        fSurname = scanner.nextLine();
        System.out.println("Please enter her age");
        fAge = scanner.nextInt();
         System.out.println("Please enter the firstname of your favourite female author");
         mFirstName = scanner.nextLine();
        System.out.println("Please enter her second name");
        mSurname = scanner.nextLine();
        System.out.println("Please enter her age");
        mAge = scanner.nextInt();
        System.out.print(fPerson);
    }
}

2 个答案:

答案 0 :(得分:2)

致电nextLine()后,添加nextInt()来电。因为nextInt()没有完成该行。因此,对nextLine()的下一次调用将返回一个空字符串。

答案 1 :(得分:1)

fAge = scanner.nextInt();不消耗该行结尾。

在此之后添加scanner.nextLine()以吸收行尾字符,它将起作用。