当前位置: 首页 > 知识库问答 >
问题:

如何将计算结果打印出来-Java

佟嘉祯
2023-03-14

所以这个程序要做的是从键盘上获取输入的id、工时和工资。然后,它将计算加班工资、定期工资和总工资。计算完后,必须将应计算的身份证、小时数、工资、加班工资、定期工资和总工资打印到输出中。

而且,雇员的属性和行为应该在雇员类中。

public class FinalExam
{ // begin class

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

    // ********** DECLARATION OF CONSTANTS **********

    // ********** DECLARATION OF VARIABLES **********

        String strout;          // output string (toString)

        int ID;                 // employee's id number
        int Hours;              // employee's hours worked
        double Wage;            // employee's wage per hour

    // ********** CREATE OBJECTS **********

        ArrayList<Employee> employeeInfo = new ArrayList();     // list of all employee's properties/objects    

    // ********** CREATE INPUT STREAMS **********

        Scanner keyboard = new Scanner(System.in);              // create a Scanner object for keyboard input.

    // ********** GET INPUT **********

            // get the employee's ID
        System.out.println("\nEnter your employee ID.");
        ID = keyboard.nextInt();                //get the input and set it to the local varaible ID
        Employee employee = new Employee(ID);   // pass your id
        //System.out.println("Employee ID: " + ID);

            // get the employee's hours worked
        System.out.println("\nEnter the amount of hours you worked this week.");
        Hours = keyboard.nextInt();             //get the input and set it to the local varaible HoursWorked
        employee.setHours(Hours);               // pass your hours
        //System.out.println("Hours worked: " + Hours);

            // get the employee's wage
        System.out.println("\nEnter your wage.");
        Wage = keyboard.nextDouble();           //get the input and set it to the local varaible Wage
        employee.setWage(Wage);                 // pass your wage
        //System.out.println("Employee wage: " + Wage);

        employeeInfo.add(employee);             // add it to the list of course

    // ********** OUTPUT **********

        System.out.println("\n\n" + employeeInfo.toString()); 

} // end main
 } // end class
public class Employee
{  // begin class

// *********** CLASS VARIABLES **********

// *********** CLASS CONSTANTS **********

    private static int MAXHOURS = 40;   // maximum hours before overime
    private static double OTRATE = 1.5; // overtime is one and a half

// ********** INSTANCE VARIABLES **********

    private int ID;                     // employee's id
    private int Hours;                  // number of hours worked
    private double Wage;                // pay per hour

    private double RegularPay;          // regular pay
    private int  OverHours;         // number of overtime hours worked
    private double OverPay;             // overtime pay
    private double GrossPay;                // gross pay

// ********** CREATE INPUT STREAMS **********

    DecimalFormat df1 = new DecimalFormat ("#####.00");     // to get two decimal places at the end of the numbers

// ********** CONSTRUCTORS ***********

    public Employee(int IDnumber)
    { // begin initialized constructor
        ID = IDnumber;          // set ID to ID number
    } // end initialized constructor

// ********** ACCESSORS **********

    public int getID()
    { // begin getID
        return ID;
    } // end getID

    public void setWage(double HourlyWage)
    { // begin setWage
        Wage = HourlyWage;
    } // end setWage

    public double getWage()
    { // begin getWage
        return Wage;
    } // end getWage

    public void setHours(int hoursWorked)
    { // begin setHours
        Hours = hoursWorked;
    } // end setHours

    public double getHours()
    { // begin getHours
        return Hours;
    } // end getHours

// ********** MUTATORS **********

    public double getOverPay()
    { // begin getOverPay
        if (Hours > MAXHOURS)
        { // begin if hours worked is bigger than MAXHOURS
            OverHours = Hours - MAXHOURS;
            OverPay = OverHours * Wage * OTRATE;
        } // end if hours worked is bigger than MAXHOURS
        else
            OverPay = 0;

        return OverPay;
    } // end getOverPay

    public double getRegularPay()
    { // begin getRegularPay
        return MAXHOURS * Wage;
    } // end getRegularPay

    public double getGrossPay()
    { // begin getGrossPay
        return RegularPay + OverPay;
    } // end getGrossPay


    public String toString()    // overrides the toString method inherited from object
    { // begin toString
        String strout = "\nId \t\t Hours \t\t Rate \t\t Regular Pay \t Overtime Pay \t Gross Pay\n";
        strout += ID + "\t " + Hours + "\t\t\t $" + (df1.format(Wage)) + "\t\t $" + (df1.format(RegularPay)) + "\t\t\t $" + (df1.format(OverPay)) + "\t\t\t $" + (df1.format(GrossPay));    
            // df1.format(double) allows me two decimal places  

        return strout;
    } // end toString

}  // end class

共有1个答案

徐友樵
2023-03-14

您正在使用OvertimePay、GrossPay和RegularPay,但没有使用它们的getter,而且这些属性还没有初始化。你应该打电话给Getters。

import java.text.DecimalFormat;

public class Employee
{  // begin class

// *********** CLASS VARIABLES **********

// *********** CLASS CONSTANTS **********

    private static int MAXHOURS = 40;   // maximum hours before overime
    private static double OTRATE = 1.5; // overtime is one and a half

// ********** INSTANCE VARIABLES **********

    private int ID;                     // employee's id
    private int Hours;                  // number of hours worked
    private double Wage;                // pay per hour

    private double RegularPay;          // regular pay
    private int  OverHours;         // number of overtime hours worked
    private double OverPay;             // overtime pay
    private double GrossPay;                // gross pay

// ********** CREATE INPUT STREAMS **********

    DecimalFormat df1 = new DecimalFormat ("#####.00");     // to get two decimal places at the end of the numbers

// ********** CONSTRUCTORS ***********

    public Employee(int IDnumber)
    { // begin initialized constructor
        ID = IDnumber;          // set ID to ID number
    } // end initialized constructor

// ********** ACCESSORS **********

    public int getID()
    { // begin getID
        return ID;
    } // end getID

    public void setWage(double HourlyWage)
    { // begin setWage
        Wage = HourlyWage;
    } // end setWage

    public double getWage()
    { // begin getWage
        return Wage;
    } // end getWage

    public void setHours(int hoursWorked)
    { // begin setHours
        Hours = hoursWorked;
    } // end setHours

    public double getHours()
    { // begin getHours
        return Hours;
    } // end getHours

// ********** MUTATORS **********

    public double getOverPay()
    { // begin getOverPay
        if (Hours > MAXHOURS)
{ // begin if hours worked is bigger than MAXHOURS
            OverHours = Hours - MAXHOURS;
            OverPay = OverHours * Wage * OTRATE;
        } // end if hours worked is bigger than MAXHOURS
        else
            OverPay = 0;

        return OverPay;
    } // end getOverPay

    public double getRegularPay()
    { // begin getRegularPay

        return MAXHOURS * Wage;
    } // end getRegularPay

    public double getGrossPay()
    { // begin getGrossPay
        return getRegularPay() + OverPay;
    } // end getGrossPay


    public String toString()    // overrides the toString method inherited from object
    { // begin toString
        String strout = "\nId \t\t Hours \t\t Rate \t\t Regular Pay \t Overtime Pay \t Gross Pay\n";
        strout += ID + "\t " + Hours + "\t\t\t $" + (df1.format(Wage)) + "\t\t $" + (df1.format(getRegularPay())) + "\t\t\t $" + (df1.format(getOverPay())) + "\t\t\t $" + (df1.format(getGrossPay()));    
            // df1.format(double) allows me two decimal places  

        return strout;
    } // end toString

}  // end class
 类似资料:
  • 嗨,我有这样的东西: 如何打印排序结果?

  • 有人能告诉我如何打印以下课程的成绩吗:

  • 问题内容: 如何获得要打印到[标准输出]的JUnit断言的结果? 我有一些像这样的测试: 这是我希望获得的打印输出格式: 是否可以使用和进行此操作?或是否存在任何,方法? 问题答案: 首先,您有两个问题,而不是一个。断言失败时,将引发异常。这样可以防止检查超过此点的任何断言。为了解决这个问题,您需要使用ErrorCollector。 其次,我不认为JUnit内置了任何方法来执行此操作。但是,您可以

  • 我正在做我的编程课介绍的作业,这是一个泡泡排序。代码可能有缺陷,但我不是在找人来解决那个问题。我的问题是,我试图打印它。我得到的条件是,该方法必须由“public static void sort(int[]array)”一行的void方法定义。因此,如果我尝试导入数组,并在main方法中使用,它将无法工作,因为我会得到一个编译器错误,说明void不适用。如果我试图将它放到main方法的循环中,它

  • 这是我在一个俄文网站上得到的代码,我曾尝试使用pair函数,但所有的尝试都失败了。请指导我如何做到这一点。

  • 我创建这段代码是为了创建随机数和运算符,但它如何计算并显示结果呢? 它现在做的是,例如打印出来:4+1-3或9*2-8等。我不知道如何计算出4+1-3或9*2-8的结果,然后打印出来。