gpt4 book ai didi

java - 如何打印计算结果 - Java

转载 作者:行者123 更新时间:2023-12-01 23:03:34 24 4
gpt4 key购买 nike

所以这个程序要做的就是从键盘上获取 ID、工时和工资的输入。然后将计算加类费、正常工资和毛工资。计算完成后,需要计算的 ID、工时、工资、加类费、正常工资和总工资必须打印到输出中。

此外,员工的属性和行为也应该属于员工类。

但是当我想打印 toString 方法时,正常工资、加类工资和总工资显示为零。知道为什么吗?

主要方法:

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

然后是 Employee 类:

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

最佳答案

您正在使用 OvertimePay、GrossPay 和 RegularPay,但没有使用它们的 getter,并且这些属性尚未初始化。您应该调用 setter/getter 。

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

关于java - 如何打印计算结果 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23163251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com