【发布时间】: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