你能告诉我为什么我会得到一个“字符串无法转换为整数”错误吗?

huangapple go评论93阅读模式
英文:

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类:

  1. public class HourlyWorker extends Employee {
  2. private int hours;
  3. private double hourlyRate;
  4. private double monthlyPay;
  5. private double overtime = (1.5 * hourlyRate);
  6. public HourlyWorker(String last, String first, String ID, double rate) {
  7. super(last, first, ID);
  8. hourlyRate = rate;
  9. }
  10. public void setHours(int hours) {
  11. this.hours = hours;
  12. }
  13. public int getHours() {
  14. return hours;
  15. }
  16. public void setHourlyRate(double rate) {
  17. this.hourlyRate = rate;
  18. }
  19. public double getHourlyRate() {
  20. return hourlyRate;
  21. }
  22. public double getMonthlyPay() {
  23. if (hours > 160) {
  24. monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
  25. } else {
  26. monthlyPay = hourlyRate * hours;
  27. }
  28. return monthlyPay;
  29. }
  30. public void increasePay(double percentage) {
  31. hourlyRate *= 1 + percentage / 100;
  32. }
  33. public void decreasePay(double percentage) {
  34. hourlyRate *= 1 - percentage / 100;
  35. }
  36. }

我正在进行测试的部分:

  1. public class TestEmployee2 {
  2. public static void main(String[] args) {
  3. Employee[] staff = new Employee[3];
  4. HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
  5. hw1.setHours(200);
  6. staff[0] = hw1;
  7. System.out.println(staff[0].getMonthlyPay());
  8. staff[0].increasePay(10);
  9. System.out.println(staff[0].getMonthlyPay());
  10. }
  11. }

输出是:

  1. 1600(初始月薪,包括40小时的加班和160小时的普通工时)
  2. 1760(月薪增加10%)

应该是:

  1. 2006
  2. 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:

  1. public class HourlyWorker extends Employee
  2. {
  3. private int hours;
  4. private double hourlyRate;
  5. private double monthlyPay;
  6. private double overtime = (1.5 * hourlyRate);
  7. public HourlyWorker(String last, String first, String ID, double rate)
  8. {
  9. super(last, first, ID);
  10. hourlyRate = rate;
  11. }
  12. public void setHours(int hours)
  13. {
  14. this.hours = hours;
  15. }
  16. public int getHours()
  17. {
  18. return hours;
  19. }
  20. public void setHourlyRate(double rate)
  21. {
  22. this.hourlyRate = rate;
  23. }
  24. public double getHourlyRate()
  25. {
  26. return hourlyRate;
  27. }
  28. public double getMonthlyPay()
  29. {
  30. if (hours > 160)
  31. {
  32. monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
  33. }
  34. else
  35. {
  36. monthlyPay = hourlyRate * hours;
  37. }
  38. return monthlyPay;
  39. }
  40. public void increasePay(double percentage)
  41. {
  42. hourlyRate *= 1 + percentage / 100;
  43. }
  44. public void decreasePay(double percentage)
  45. {
  46. hourlyRate *= 1 - percentage / 100;
  47. }
  48. }

What I'm testing with:

  1. public class TestEmployee2
  2. {
  3. public static void main(String[] args)
  4. {
  5. Employee [] staff = new Employee[3];
  6. HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
  7. hw1.setHours(200);
  8. staff[0] = hw1;
  9. System.out.println(staff[0].getMonthlyPay());
  10. staff[0].increasePay(10);
  11. System.out.println(staff[0].getMonthlyPay());
  12. }
  13. }
  14. Output is:
  15. 1600 (initial monthly rate, with 40 overtime hours and 160 regular hours)
  16. 1760 (10% increase to the monthlyPay)
  17. Should be:
  18. 2006
  19. 22

06.6

答案1

得分: 1

正如@NomadMaker提到的,问题出在你的addArtist方法中。
你当前的方法:

  1. public void addArtist(String artistName, String genre)
  2. {
  3. this.artists.add(artist, genre);
  4. }

请记住,this.artists是一个可以存储Artist类型对象的列表。因此,你应该使用新参数创建一个新的艺术家对象。类似这样:

  1. public void addArtist(String artist, String genre)
  2. {
  3. this.artists.add(new Artist(artist, genre));
  4. }

正如你可能猜到的,你没有一个接受两个参数(应该接受姓名和流派)的Artist构造函数。因此,你应该将这个构造函数添加到你的代码中:

  1. public Artist(String name, String genre) {
  2. this.name = name;
  3. this.genre = genre;
  4. }

错误解释:

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:

  1. public void addArtist(String artistName, String genre)
  2. {
  3. this.artists.add(artist, genre);
  4. }

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:

  1. public void addArtist(String artist, String genre)
  2. {
  3. this.artists.add(new Artist(artist, genre));
  4. }

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:

  1. public Artist(String name, String genre) {
  2. this.name = name;
  3. this.genre = genre;
  4. }

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).

huangapple
  • 本文由 发表于 2020年10月7日 07:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64235096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定