[英]Can you tell me why I'm getting a "string cannot be converted to int" error
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.
一切正常 - 我只需要计算超过 160 小时的任何每月小时数,以正常小时费率的 1.5 倍支付。 My math seems sound and calculates fine:
我的数学看起来很合理并且计算得很好:
((hours - 160) * overtime) + (160 * hourlyRate) ((小时 - 160) * 加班时间) + (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.但我不知道我是否将这个 if 语句放在正确的方法中,或者它是否应该是一个 if 语句。 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 06.6
As @NomadMaker mentioned, the problem is in your addArtist
method.正如@NomadMaker 所提到的,问题出在您的
addArtist
方法中。 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
.请记住,
this.artists
是一个列表,可以存储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).您可能会猜到,您没有带有两个参数的Artist构造函数(应该接受名称和流派)。 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). artists
是一个列表,您在调用this.artist.add(artist, genre)
正在调用一个属于具有此签名的列表集合的方法: add(int index, Artist artist)
索引将是索引放置艺术家(如果订单对您很重要)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.