【问题标题】:Not able to input the first string in a loop, it always starts from 2nd input in java无法在循环中输入第一个字符串,它总是从java中的第二个输入开始
【发布时间】:2020-12-16 09:23:59
【问题描述】:

我创建了一个 Employee 类,我只在其中传递值并打印数组。你可以看到:

package practicequestion;

import java.util.Scanner;
import practicequestion.employeeq.Employee;

public class EmployeeMain {
    public static void main(String[] args) {
        Employee[] obj = new Employee[3];
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of inputs you want ");
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {

            System.out.println("Enter FirstName");
            String firstname = sc.nextLine();
            System.out.println("Enter LastName");
            String lastname = sc.nextLine();
            System.out.println("Enter Email");
            String email = sc.nextLine();
            System.out.println("Enter Salary");
            int salary = sc.nextInt();
            System.out.println("Enter Mobile Number");
            long mobileno = sc.nextLong();
            System.out.println("Enter Experience");
            int empexp = sc.nextInt();
            System.out.println("Enter Employee Id");
            int empid = sc.nextInt();
            if (i < n) {
                obj[i] = new Employee(firstname, lastname, email, salary, empid, empexp, mobileno);
            }
        }
        for (int i = 0; i < 3; i++) {
            System.out.println("Employee " + i +  " values are: ");
            obj[i].Display();
        }
        if (sc != null) {
            sc.close();   
        }
    }

}

但我无法输入名字,因为每当我运行程序时,它都会从姓氏开始输入。它的类是这样的:

package practicequestion.employeeq;

public class Employee {
   String FirstName;
   String LastName;
   String Email;
   int Salary;
   long MobileNo;
   int EmployeeExp;
   int EmployeeId;

    public Employee(String FirstName,String LastName,String Email,int Salary,int EmployeeId,int EmployeeExp,long MobileNo) {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Email = Email;
        this.Salary = Salary;
        this.EmployeeId = EmployeeId;
        this.EmployeeExp = EmployeeExp;
        this.MobileNo = MobileNo;
    }
    public void Display() {
        System.out.println(FirstName +"\n" + LastName + "\n" + Email + "\n" + Salary + "\n" + EmployeeId +"\n" + EmployeeId +"\n" + MobileNo);
    }
}

谁能解决这个问题?

【问题讨论】:

    标签: java arrays class for-loop oop


    【解决方案1】:

    试试这个。我将记录我所做的更改。

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of inputs you want ");
    int n = sc.nextInt();
    // allocate the Employee array after you get the number of inputs.
    Employee[] obj = new Employee[n];
    
    for (int i = 0; i < n; i++) {
    
        // get the newline out of the buffer here. That also
        // takes care of the `nextInt` problem at the end of this loop
        sc.nextLine();
        
        System.out.println("Enter FirstName");
        String firstname = sc.nextLine();
        System.out.println("Enter LastName");
        String lastname = sc.nextLine();
        System.out.println("Enter Email");
        String email = sc.nextLine();
        System.out.println("Enter Salary");
        int salary = sc.nextInt();
        System.out.println("Enter Mobile Number");
        long mobileno = sc.nextLong();
        System.out.println("Enter Experience");
        int empexp = sc.nextInt();
        System.out.println("Enter Employee Id");
        int empid = sc.nextInt();
        // you don't need to verify `i` here.  The for loop does that
        // for you
        obj[i] = new Employee(firstname, lastname, email, salary,
                empid, empexp, mobileno);
        
    }
    
    // use the number of inputs `n` to limit the loop
    for (int i = 0; i < n; i++) {
        System.out.println("Employee " + i + " values are: ");
        obj[i].Display();
    }
    
    

    关闭扫描仪通常不是一个好习惯。

    【讨论】:

      【解决方案2】:

      这里已经回答了这个问题:Scanner is skipping nextLine() after using next() or nextFoo()?

      基本上问题在于您在进入循环之前调用的 Scanner.nextInt() 。 Scanner.nextInt() 仅读取用户输入的下一个 Integer,而不是整行,因此当您在 Scanner.nextLine() 直接接受您输入整数的行的剩余(和空)部分之后调用它时。

      要解决这个问题,您要么必须调用 Scanner.nextLine() 一次,要么使用 Scanner.nextLine() 获取 Integer,然后将其转换为 Integer。

      【讨论】:

      • 感谢您的建议。现在我也可以输入它但无法打印名字。请指导我如何做到这一点?
      猜你喜欢
      • 2017-06-22
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2021-08-17
      • 2020-12-29
      • 1970-01-01
      相关资源
      最近更新 更多