简体   繁体   English

你能告诉我为什么我收到“字符串无法转换为 int”错误

[英]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.

相关问题 我的代码出现错误:不兼容的类型:int无法转换为布尔线:6 - I'm getting an error for my code: incompatible types: int cannot be converted to boolean line:6 为什么这段代码给我错误“无法将imageicon转换为int”? - Why does this code gives me the error “imageicon cannot be converted to int”? 我收到此错误消息并了解原因:不能应用于给定类型; 必需:未找到增强:int,int - I'm getting this error message and understand why: cannot be applied to given types; required: no augments found: int, int 我在defaultWriteObject处收到NotActiveException,我不知道为什么 - I'm getting a NotActiveException at defaultWriteObject and I can't tell why 任何人都可以告诉我为什么我得到SQL * PLUS无效标识符错误? - Can anyone tell me why I am getting a SQL*PLUS invalid identifier error? 出现“运算符‘&&’不能应用于‘布尔值’、‘int’”错误,我不确定为什么 - Getting an "Operator '&&' cannot be applied to 'boolean', 'int'" error and I'm unsure why 为什么我会得到“不兼容的类型:对象无法转换为字符串”? - Why am I getting, “incompatible types: Object cannot be converted to String” with this? 错误:不兼容的类型:int 无法转换为 String - error: incompatible types: int cannot be converted to String 错误:无法比较的类型:字符串不能转换为int - Error:incomparable types: String cannot be converted into int “字符串无法转换为int”红线错误 - "String cannot be converted to int" red line error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM