我目前正在学习 Java,但不太确定如何做到这一点。
目前我有一个员工班
public class employee {
private int empID;
private String empName;
private String job;
//constructors;
public employee(){
this.empID = 0;
this.empName = "";
this.job = "";
}
public employee(int empID, String empName, String job){
this.empID = empID;
this.empName = empName;
this. job = job;
}
//gets;
public int getEmpID(){
return this.empID;
}
public String getEmpName(){
return this.empName;
}
public String getJob(){
return this.job;
}
//sets;
public void setEmpID(int empID){
this.empID = empID;
}
public void setEmpName(String empName){
this.empName = empName;
}
public void setJob(String job){
this.job = job;
}
}
在我的主目录中,我创建了一个大小为 2 的员工类型的对象数组,然后只增加数组计数,同时让用户输入一些基本信息,然后另一个循环打印输入的所有信息。
import java.util.Scanner;
public class employeeArray {
public static void p(String s) {System.out.println(s);}
public static void main(String args[]){
Scanner k = new Scanner(System.in);
int empID, size=0, x=0;
String empName;
String job;
employee [] employees = new employee[2];
for(size = 0; size < 2; size++)
{
p("Employee "+(size+1));
p("Please enter employee ID number: ");
empID = k.nextInt(); k.nextLine();
p("Please enter your name: ");
empName = k.nextLine();
p("Please enter your job role: ");
job = k.nextLine();
employees[size] = new employee();
employees[size].setEmpID(empID);
employees[size].setEmpName(empName);
employees[size].setJob(job);
}
for(x=0; x<2;x++){
p("Hello employee: "+employees[x].getEmpName()+" your job role is "+employees[x].getJob()+
" your log in ID is 0800"+employees[x].getEmpID());
}
}
}
我了解如何通过输入填充对象的每个实例,但说我想将大小从 2 增加到 20,但已经有前 10 个“预输入”,所以我可以显示我目前拥有但仍然允许输入的 10 个还有10个。
附言。抱歉我的冗长问题、代码块和术语不佳/缺乏。我是新手。