如何从用户获取值并将对象存储到数组中

时间:2015-03-15 08:46:28

标签: java

我创建了一个名为Employee的类,我为它设置了变量。现在我正在为该类创建一个对象。在这个对象中,我想创建一个名为“GetValue”的函数,它应该初始化我在类Employee中创建的变量。我希望它能得到例如员工姓名,员工编号等等。我已经开始编码但是我坚持这个......

//main class
public class Employee {

    //variable declaration

        private String EName, EDesig; 
        private double BSal=0.0, HA=0.0, GSal=0.0;
        private int EmpNo;


    // constructor;
     public void Employee (String EnNameParameter, String EDesigParameter, double BSalParameter, double HAParameter, double GSalParameter, int EmpNoParameter)
    {
        this.EName = EnNameParameter;
        this.EDesig = EDesigParameter;
        this.BSal = BSalParameter;
        this.HA = HAParameter;
        this.GSal = GSalParameter;
        this.EmpNo = EmpNoParameter;

    } // End constructor;

     // Get methods;
     public String getEName(){
            return this.EName;
        }
     public String getEDesig(){
            return this.EDesig;
        }
     public double getBSal(){
            return this.BSal;
        }
     public double getHA(){
            return this.HA;
        }
     public double getGSal(){
            return this.GSal;
        }
     public int getEmpNo(){
            return this.EmpNo;
        }


     //Set methods;
     public void setEName(String Ename){
         this.EName = EName;
     }
     public void setEDesig(String EDesig){
         this.EDesig = EDesig;
     }
     public void setBSal(double BSal){
         this.BSal = 0.0;
     }
     public void setHA(double HA){
         this.HA = 0.0;
     }
     public void setGSal(double GSal){
         this.GSal = 0.0;
     }
     public void setEmpNo(int EmpNo){
         this.EmpNo = EmpNo;
     }
}

import java.util.Scanner; 
public class test {

    public static void main(String[] args) {
        // create object


        Employee a = new Employee();
        Employee b = new Employee();
        Employee c = new Employee();


        Scanner input = new Scanner (System.in);
        System.out.println("Enter The Number of Employee");
        int number = input.nextInt();          

        int[] N = new int[3];                  // instanitate array
        int i;

        for(i=0; i<number; i++){

        }

        getValues();
        CalculateSalary();
        DisplayValues();


        }
}

2 个答案:

答案 0 :(得分:1)

这样的东西?

// make an empty list
List<Employee> employees = new ArrayList<>();

// make a scanner that reads from stdin
Scanner sc = new Scanner(System.in);

System.out.print("How much employees does your company have?");
int n = sc.nextInt() // nr of employees

for(int i = 0; i < n; i++) { // loop over all employees
    // read the information of the ith employee
    System.out.print("What is the name of the i th employee?");
    String name = sc.next();
    int age = sc.nextInt() 

    // make an employee object
    Employee employee = new Employee(name, age);
    employees.add(employee); // add the employee to the list
}
// Now you have a list with n employees

// to print all employees on stdout
for(Employee e : employees) {
    System.out.println(e.toString());  
    // make a toString() method in you Employee class, 
    // else this will not work properly
}

答案 1 :(得分:0)

如何为用户输入的数字创建对象数组。以下代码将为您提供帮助:

 public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        System.out.println("Enter The Number of Employee");
        int number = input.nextInt();          


        Employee[] emp = new Employee[number];  // create object array for no of employees
        for(int i=0; i<number; i++){
            emp[i].setBSal(100);
            emp[i].setEDesig(null);
            emp[i].setEName("Ann");
            emp[i].setEmpNo(234);
            emp[i].setGSal(34);
        }

        getValues();
        CalculateSalary();
        DisplayValues();
        }

这是主要课程。您可以使用用户输入来设置每种方法。