for循环跳过扫描仪输入

时间:2019-08-09 11:22:08

标签: java

我是Java的新手,我一直想了解Java中的循环。我有下面的代码供试用,问题是主方法中的for循环在第一次迭代中打印出所有正确的内容,如下所示:

CMD>Please enter the name of person 1
CMD>ber
CMD>
CMD>sex of person1
CMD>male
CMD>
CMD>age of person1
CMD>23

但是在第二次迭代中 这发生了

CMD>Please enter the name of person 2
CMD>sex of person2
CMD>

跳过名字查询 代码在下面

我已经经历了所有的试验和调试

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

    person [] persons= new person[3];
    Scanner input= new Scanner(System.in);

        for (int i = 0; i <=2 ; i++) {

            persons[i]= new person();
            System.out.println(" Please enter the name of person " + (i+1));
            persons[i].P_name = input.nextLine();
            System.out.println(" sex of person" + (i+1));
            persons [i].P_sex= input.nextLine();
            System.out.println(" age of person" + (i+1));
            persons[i].P_age =input.nextInt();

        }
        ...

我想输入多个名称。有一班人

String P_name, P_sex;
int P_age; 

metho void P_data () 
  System.out.println(" The person named " + P_name + "is " + P_age + " years old and is a " + P_sex);

0 个答案:

没有答案