gpt4 book ai didi

java - 为什么我的 String[] 出现错误 "no suitable constructor found"?

转载 作者:行者123 更新时间:2023-12-05 08:59:09 25 4
gpt4 key购买 nike

错误是 TestPayroll 没有“找到合适的构造函数”:
工资表 payroll = new Payroll(name, weeksPay);

构造函数应该是什么?我认为它应该在 Payroll 类中。

我想显示小蒂姆、布拉德皮特和麦当娜本周的工资。

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;
}

}

最佳答案

Payroll 有以下构造函数

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

    public Payroll() {/* some */}

并且您将 String[] 作为第一个参数传递

关于java - 为什么我的 String[] 出现错误 "no suitable constructor found"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17620320/

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