有人能告诉我为什么我会收到一个名为不兼容类型的错误吗?这是完整错误:PayrollTest.java:26: error: incompatible types: possible lossy conversion from double to int.
01: import java.util.Scanner;
02:
03: public class PayrollTest{
04:
05: public static void assignEmployeeHoursAndPayrates( Payroll payrollObject ) {
06: Scanner keyboard = new Scanner( System.in );
07: int employeeHours;
08: double employeePayRate;
09:
10: for( int employeeIndex = 0; employeeIndex < payrollObject.getAllEmployeeIds().length; employeeIndex++ ) {
11: System.out.println( "Please enter the hours for employee " + payrollObject.getAllEmployeeIds()[ employeeIndex ] );
12: employeeHours = keyboard.nextInt();
13: while( employeeHours < 0 ) {
14: System.out.println( "Please enter POSITIVE hours for employee " +
15: payrollObject.getAllEmployeeIds()[ employeeIndex ] );
16: employeeHours = keyboard.nextInt();
17: }
18: payrollObject.setEmployeeHours( employeeHours, employeeIndex );
19: System.out.println( "What's the payrate for employee " + payrollObject.getAllEmployeeIds()[ employeeIndex ] );
20: employeePayRate = keyboard.nextDouble();
21: while( employeePayRate < 6.00 ) {
22: System.out.println( "What's the payrate for employee " +
23: payrollObject.getAllEmployeeIds()[ employeeIndex ] + "( $6 or ABOVE please)" );
24: employeePayRate = keyboard.nextDouble();
25: }
26: payrollObject.setEmployeePayRate( employeePayRate, employeeIndex );
27: }
28: }
29:
30: public static void displayEmployeeWages( Payroll payrollObject) {
31: for( int employeeIndex = 0; employeeIndex < payrollObject.getAllEmployeeIds().length; employeeIndex++ ) {
32: System.out.println( "The employee with id number " + payrollObject.getAllEmployeeIds()[ employeeIndex ] +
33: " worked for " + payrollObject.getAllEmployeeHours()[ employeeIndex ] +
34: " hours and earned " +
35: String.format( "$%,.2f" , payrollObject.getAllEmployeeWages()[ employeeIndex ] ) );
36: }
37: }
38:
39: public static void main( String [ ] args ) {
40: Payroll employeeInfo = new Payroll();
41: assignEmployeeHoursAndPayrates( employeeInfo );
42: employeeInfo.calculateWages();
43: displayEmployeeWages( employeeInfo );
44:
45: int singleEmployeeId = 7895122;
46:
47: if( employeeInfo.getEmployeeWagesById( singleEmployeeId ) != -1 ) {
48: System.out.println( "The single employee with id " + singleEmployeeId + " earned " +
49: String.format( "$%,.2f", employeeInfo.getEmployeeWagesById( singleEmployeeId ) ) );
50: } else{
51: System.out.println( "The single employee with id " + singleEmployeeId + " wasn't found" );
52: }
53: }
54: }
答案 0 :(得分:0)
可能的解决方法
在薪资课程中,转到方法 setEmployeePayRate()
现在这样做。将 int 更改为 double
void setEmployeePayRate(double employeePayRate, int employeeIndex)
{
// your code goes here
}
您还需要在方法
中将一些变量从int更改为double