英文:
Can you tell me why I'm getting a "string cannot be converted to int" error
问题
以下是翻译好的部分:
我有一个作业,要求我计算下面代码中的内容。所有内容都正常工作 - 我只需要计算超过160小时的任何月工时,按正常小时费率的1.5倍支付。我的数学似乎是正确的,可以正确计算:
((hours - 160) * overtime) + (160 * hourlyRate)
但我不知道是否应该将这个if语句放在正确的方法中,或者它是否应该是一个if语句。在此之前,我的increase/decreasePay方法是有效的,并且它们需要保留。我删除了一些内容,以便阅读更容易。
HourlyWorker类:
public class HourlyWorker extends Employee {
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);
public HourlyWorker(String last, String first, String ID, double rate) {
super(last, first, ID);
hourlyRate = rate;
}
public void setHours(int hours) {
this.hours = hours;
}
public int getHours() {
return hours;
}
public void setHourlyRate(double rate) {
this.hourlyRate = rate;
}
public double getHourlyRate() {
return hourlyRate;
}
public double getMonthlyPay() {
if (hours > 160) {
monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
} else {
monthlyPay = hourlyRate * hours;
}
return monthlyPay;
}
public void increasePay(double percentage) {
hourlyRate *= 1 + percentage / 100;
}
public void decreasePay(double percentage) {
hourlyRate *= 1 - percentage / 100;
}
}
我正在进行测试的部分:
public class TestEmployee2 {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
hw1.setHours(200);
staff[0] = hw1;
System.out.println(staff[0].getMonthlyPay());
staff[0].increasePay(10);
System.out.println(staff[0].getMonthlyPay());
}
}
输出是:
1600(初始月薪,包括40小时的加班和160小时的普通工时)
1760(月薪增加10%)
应该是:
2006
22
英文:
I have an assignment which asks for everything I have in the code below. That all works fine - I just need to calculate any monthly hours over 160 hours to be paid at 1.5 times the normal hourly rate. My math seems sound and calculates fine:
((hours - 160) * overtime) + (160 * hourlyRate)
But I dont know if I'm putting this if statement in the right method or if it even should be an if statement. My increase/decreasePay methods are working prior to this and they need to stay. I removed some things so it's easier to read.
HourlyWorker Class:
public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);
public HourlyWorker(String last, String first, String ID, double rate)
{
super(last, first, ID);
hourlyRate = rate;
}
public void setHours(int hours)
{
this.hours = hours;
}
public int getHours()
{
return hours;
}
public void setHourlyRate(double rate)
{
this.hourlyRate = rate;
}
public double getHourlyRate()
{
return hourlyRate;
}
public double getMonthlyPay()
{
if (hours > 160)
{
monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
}
else
{
monthlyPay = hourlyRate * hours;
}
return monthlyPay;
}
public void increasePay(double percentage)
{
hourlyRate *= 1 + percentage / 100;
}
public void decreasePay(double percentage)
{
hourlyRate *= 1 - percentage / 100;
}
}
What I'm testing with:
public class TestEmployee2
{
public static void main(String[] args)
{
Employee [] staff = new Employee[3];
HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
hw1.setHours(200);
staff[0] = hw1;
System.out.println(staff[0].getMonthlyPay());
staff[0].increasePay(10);
System.out.println(staff[0].getMonthlyPay());
}
}
Output is:
1600 (initial monthly rate, with 40 overtime hours and 160 regular hours)
1760 (10% increase to the monthlyPay)
Should be:
2006
22
06.6
答案1
得分: 1
正如@NomadMaker提到的,问题出在你的addArtist
方法中。
你当前的方法:
public void addArtist(String artistName, String genre)
{
this.artists.add(artist, genre);
}
请记住,this.artists
是一个可以存储Artist
类型对象的列表。因此,你应该使用新参数创建一个新的艺术家对象。类似这样:
public void addArtist(String artist, String genre)
{
this.artists.add(new Artist(artist, genre));
}
正如你可能猜到的,你没有一个接受两个参数(应该接受姓名和流派)的Artist构造函数。因此,你应该将这个构造函数添加到你的代码中:
public Artist(String name, String genre) {
this.name = name;
this.genre = genre;
}
错误解释:
artists
是一个列表,当调用this.artist.add(artist, genre)
时,你实际上在调用属于列表集合的一个方法,该方法具有这样的签名:add(int index, Artist artist)
,index将是放置艺术家的索引(如果顺序对你很重要的话)。
英文:
As @NomadMaker mentioned, the problem is in your addArtist
method.
Your current method:
public void addArtist(String artistName, String genre)
{
this.artists.add(artist, genre);
}
Remember that this.artists
is a list which can store Objects of type Artist
.
Therefore you should create a new artist with the new parameters. Something like:
public void addArtist(String artist, String genre)
{
this.artists.add(new Artist(artist, genre));
}
As you might be guessing, you do not have a constructor of Artist with two parameters (should accept name, and genre). Therefore, you should add this constructor to your code:
public Artist(String name, String genre) {
this.name = name;
this.genre = genre;
}
Error explanation:
artists
is a list, what you are doing when calling this.artist.add(artist, genre)
is calling a method which belongs to the list collection that has this signature: add(int index, Artist artist)
the index will be the index to place the artist (if the order matters for you).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论