猿问

计算 20 名工人平均工资的 Java 程序

我已经坚持了一段时间,一个计算 20 名工人平均工资的 Java 程序,这是我到目前为止想出的。………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ......


import java.util.Scanner;


class Employee

{

    int age;

    String name, address, gender;

    Scanner get = new Scanner(System.in);

    Employee()

    {

        System.out.println("Enter Name of the Employee:");

        name = get.nextLine();

        System.out.println("Enter Gender of the Employee:");

        gender = get.nextLine();

        System.out.println("Enter Address of the Employee:");

        address = get.nextLine();

        System.out.println("Enter Age:");

        age = get.nextInt();

    }


    void display()

    {

        System.out.println("Employee Name: "+name);

        System.out.println("Age: "+age);

        System.out.println("Gender: "+gender);

        System.out.println("Address: "+address);

    }

}


class fullTimeEmployees extends Employee

{

    float salary;

    int des;

    fullTimeEmployee()

    {

        System.out.println("Enter Designation:");

        des = get.nextInt();

        System.out.println("Enter Salary:");

        salary = get.nextFloat();

    }

    void display()

    {

        System.out.println("=============================="+"\n"+"Full Time Employee Details"+"\n"+"=============================="+"\n");

        super.display();

        System.out.println("Salary: "+salary);

        System.out.println("Designation: "+des);

    }

}


class partTimeEmployees extends Employee

{

    int workinghrs, rate;

    partTimeEmployees()

    {

        System.out.println("Enter Number of Working Hours:");

        workinghrs = get.nextInt();

    }

    void calculatepay()

    {

        rate = 8 * workinghrs;

    }


慕码人2483693
浏览 184回答 2
2回答

慕田峪4524236

在 Employee 中添加一个返回工资的抽象方法:public abstract int getSalary();并在每个子类中实现它:// class names start with upper case characterclass FullTimeEmployee extends Employee{    // don't use float, represent money in cents (and you should maybe represent the currency separately)    int salary;    // 'des' is not clear    int designtation;    fullTimeEmployee()    {        System.out.println("Enter Designation:");        designtation = get.nextInt();        System.out.println("Enter Salary:");        salary = get.nextInt();    public abstract int getSalary() {        return salary;    }为什么是rate = 8 & workingHours?要计算平均值,请跟踪总工资和计算的员工人数,同时迭代每个员工:    int count = 0;    int totalSalary = 0;        Employee emp;     while (emp = getEmployee() != null) {        count++;             totalSalary += emp.getSalary();    }    double avgSalary = totalSalary / count;    System.out.println("Avg salary: " + avgSalary);}private Employee getEmployee() {    Employeee emp = null;    System.out.println("Part time or full time employee? any other key to quit (p/f):");    String input = get.nextString();    if ("f".equalsIgnoreCase(input)) {        System.out.println("================================"+"\n"+"Enter     Full Time Employee Details"+"\n"+"================================"+"\n");        emp = new FullTimeEmployee();    }    else if ("p".equalsIgnoreCase(input)) {        emp = new PartTimeEmployee();        System.out.println("================================"+"\n"+"Enter Part Time Employee Details"+"\n"+"================================"+"\n");    }    return emp;}    

蛊毒传说

将构造函数更改fullTimeEmployee为fullTimeEmployeesclass fullTimeEmployees extends Employee{&nbsp; &nbsp; float salary;&nbsp; &nbsp; int des;&nbsp; &nbsp; fullTimeEmployees()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Designation:");&nbsp; &nbsp; &nbsp; &nbsp; des = get.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Salary:");&nbsp; &nbsp; &nbsp; &nbsp; salary = get.nextFloat();&nbsp; &nbsp; }&nbsp; &nbsp; void display()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("=============================="+"\n"+"Full Time Employee Details"+"\n"+"=============================="+"\n");&nbsp; &nbsp; &nbsp; &nbsp; super.display();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Salary: "+salary);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Designation: "+des);&nbsp; &nbsp; }}这适用于一名员工。如果你想为 20 名员工做同样的事情,那么使用循环;&nbsp; &nbsp; &nbsp; public static void main(String args[])&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; fullTimeEmployees[] fullTimeEmployees= new fullTimeEmployees[20];&nbsp; &nbsp; &nbsp; &nbsp; partTimeEmployees[] partTimeEmployees= new partTimeEmployees[20];&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<20;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("================================"+"\n"+"Enter Full Time Employee Details"+"\n"+"================================"+"\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fullTimeEmployees[i] = new fullTimeEmployees();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("================================"+"\n"+"Enter Part Time Employee Details"+"\n"+"================================"+"\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partTimeEmployees[i] = new partTimeEmployees();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("================================"+"\n"+"Full Time Employee Details"+"\n"+"================================"+"\n");&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0 ;i<20;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fullTimeEmployees[i].display();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("================================"+"\n"+"Part Time Employee Details"+"\n"+"================================"+"\n");&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0 ;i<20;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partTimeEmployees[i].calculatepay();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partTimeEmployees[i].display();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答