For循环在第二个循环中跳过第一个输入提示

时间:2020-07-06 09:25:02

标签: java

以下代码旨在通过用户输入(名字,姓氏,职务和性别)填充员工对象的数组列表。在第二个循环中,代码再次要求输入名字,就像预期的那样,但是不允许用户输入名字,而是提示用户输入姓氏。我在做什么错了?

for(int i = 0; i < 2; i++){ //LOOP MESSES UP SOMEHOW
    //Get employee object first Name
    System.out.println("First name: ");
    fName = kb.nextLine();

    //Get employee object last Name
    System.out.println("\nLast name: ");
    lName = kb.nextLine();

    //Get employee object Job Title
    System.out.println("\nJob Title: ");
    job = kb.nextLine();

    //Set employee object sex
    while(loopControl){

        System.out.println("\nSex: (Enter M for Male or F for Female)");
        empSex = kb.next().charAt(0);

        //Validate sex value input
        if(empSex == 'M' || empSex == 'F'){
            loopControl = false;
        }
        else{
            System.out.println("Invalid entry. Enter M for Male or F for Female \n");
        }
    }

    //Add populated employee object to ArrayList
    employees.add(new EmployeeRegistry(fName, lName, job, empSex));
}

0 个答案:

没有答案
相关问题