java不会等待第二次输入

时间:2016-12-01 13:29:10

标签: java

我正在学习java并尝试运行此代码:

import java.util.Scanner;

class Person {
    String name;
    int age;

}

public class MyClass {
    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    Person person1 = new Person();
    System.out.println("Input person1 age");
    person1.age = scan.nextInt();
    System.out.println("Input person1 name");
    person1.name = scan.nextLine();
    System.out.println(person1.name + " is " + person1.age);

    Person person2 = new Person();
    System.out.println("Input person2 age");
    person2.age = scan.nextInt();
    System.out.println("Input person2 name");
    person2.name = scan.nextLine();
    System.out.println(person2.name + " is " + person2.age);

    }
}

当我运行代码时,我输入person1的年龄,但随后它跳过提示输入person1的名称并将其留空并返回“是18”,例如,对于person2输入提示符相同。我将如何格式化此代码以使其顺利运行我无法弄明白并且将非常感谢帮助

0 个答案:

没有答案