1

错误是没有“找到合适的构造函数”为 TestPayroll 这个语句: Payroll payroll = new Payroll(name, weekPay); 构造函数应该是什么,我假设它应该在 Payroll 类中。

我想显示 Tiny Tim、Brad Pitt 和 Madonna 的一周工资。谢谢!

import javax.swing.JOptionPane;

public class TestPayroll {

    private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
    private double [] payRate = {100.25, 150.50, 124.25};
    private double [] hrsWorked = {40, 35, 36};
    private double weeksPay;

    //Payroll object
    Payroll payroll = new Payroll(name, weeksPay);


    public static void main(String[] args) {

    //Display weekly pay
    JOptionPane.showMessageDialog(null, "%s's pay for the week is: $%.2f\n", payroll[0].getName(), payroll[0].getWeeksPay()); 

    }   

}

public class Payroll {

    public static void main(String[] args) {
        }

        private String name;
        private double payRate;
        private double hrsWorked;
        private double weeksPay;

        //default constructor
        public Payroll() {
            this.name = name;
            this.payRate = payRate;
            this.hrsWorked = hrsWorked;
            this.weeksPay = weeksPay;
        }

        //Payroll constructor
        public Payroll(String name, double weeksPay) {
            this.name = name;
            this.weeksPay = weeksPay;
        }

        //return name
        public String getName() {
            return name;
        }

        //set name
        public void setName(String name) {
            this.name = name;
        }

        //return pay rate
        public double getPayRate() {
            return payRate;
        }

        //set pay rate
        public void setPayRate(double payRate) {
            this.payRate = payRate;
        }

        //return hours worked for the week
        public double getHrsWorked() {
            return hrsWorked;
        }

        //set hours worked for the week
        public void setHrsWorked(double hrsWorked) {
            this.hrsWorked = hrsWorked;
        }

        //find week's pay
        public double getWeeksPay(double weeksPay) {
            double weeksPay = payRate * hrsWorked;
            return weeksPay;
        }   

}
4

3 回答 3

8

您有以下构造函数Payroll

    public Payroll(String name, double weeksPay) {/* some code */}

    public Payroll() {/* some */}

String[]你作为第一个参数传递

于 2013-07-12T17:08:38.567 回答
1
   //Payroll constructor. your argument types is String and double.
    public Payroll(String name, double weeksPay) {
        this.name = name;
        this.weeksPay = weeksPay;
    }

  //Payroll object
  Payroll payroll = new Payroll(name, weeksPay); // you are using String[] array and double as in put arguments.
于 2013-07-12T17:12:15.797 回答
0

这是您正在调用的方法,它将参数作为单个字符串:

//Payroll constructor
        public Payroll(String name, double weeksPay)

以下是您正在拨打的电话:

    //Payroll object
    Payroll payroll = new Payroll(name, weeksPay);

其中对象被引用名称作为字符串数组对象,而不是传递单个字符串:

    private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};

尝试使用枚举器逐个传递值。或者您可以为字符串数组创建一个构造函数,其中需要将相同的逻辑添加到构造函数而不是 main 方法中。

于 2020-07-19T13:58:09.670 回答