0

关于如何显示员工的计算工资,我又遇到了一个问题。当我输入小时工资时,总工资不会显示。

这是我到目前为止所做的..

WageCalcu.java

  public class WageCalcu
  {
      private String employeeName;
      private int hours;
      private double rate, pay;

      public void setEmployeeName ( String name )
      {
          employeeName = name;
      }
      public String getEmployeeName()
      {
          return employeeName;
      }
      public double calculatePay( int hours, double rate )
      {
          if ( hours > 40 )
          { 
              int extraHours = hours - 40;
              pay = ( 40 * rate ) + ( extraHours * rate );
          }
          else pay = hours * rate;

          return pay;
      }
      public void displayEmployee()
      {
          System.out.printf( "Employee's name: %s", getEmployeeName() );
          System.out.printf( "\nGross Salary: ", + pay );
      }
  }

Employee.java

  import java.util.Scanner;
  public class Employee 
  {    
      public static void main(String[] args) 
      {
          Scanner input = new Scanner( System.in);
          WageCalcu employee = new WageCalcu();

          System.out.print( "Enter Employee %s name: " );
          String name = input.nextLine();
          employee.setEmployeeName( name );

          System.out.print( "Enter how many hours worked: " );
          int hours = input.nextInt();

          System.out.print( "Enter hourly rate: " );
          double rate = input.nextInt();

          employee.calculatePay( hours, rate );
          employee.displayEmployee();

          System.out.println();

      }
  }
4

4 回答 4

7

我确定你的意思是:

System.out.printf("\n总工资: %f", pay);

还有一件事

double rate = input.nextInt();

应该

double rate = input.nextDouble(); 

如果你真的期待一个实数。

于 2012-06-20T07:06:42.587 回答
0

你错过了 %sprintf( "\nGross Salary: ", + pay );

于 2012-06-20T07:08:24.337 回答
0

这次聚会晚了五年,但我会说我的。

您已将其设置为确定员工有多少加班时间,但您没有计算他们的加班工资。

你有什么:

pay = ( 40 * rate ) + ( extraHours * rate );

它应该是什么:

pay = ( 40 * rate ) + ( extraHours * rate * 1.5);
于 2017-10-10T17:23:59.873 回答
0

我会说:System.out.printf( "\nGross Salary: %.2f", pay); 显示2位小数。

于 2017-08-29T02:00:18.613 回答