![](/img/trans.png)
[英]Error : Incompatible types Possible lossy conversion from double to int
[英]error: incompatible types: possible lossy conversion from double to int
public class FoodItem {
private String name;
private int calories;
private int fatGrams;
private int caloriesFromFat;
public FoodItem(String n, int cal, int fat) {
name = n;
calories = cal;
fatGrams = fat;
}
public void setName(String n) {
name = n;
}
public void setCalories(int cal) {
calories = cal;
}
public void setFatGrams(int fat) {
fatGrams = fat;
}
public String getName() {
return name;
}
public int getCalories() {
return calories;
}
public int getFatGrams() {
return fatGrams;
}
public int getCaloriesFromFat() {
return (fatGrams * 9.0);
}
public int getPercentFat() {
return (caloriesFromFat / calories);
}
// Ignore for now
public String toString() {
return name + calories + fatGrams;
}
}
我的指令是“包括一个名为卡路里来源的方法,该方法返回一个整数,其中脂肪占总卡路里的卡路里数量(脂肪克* 9.0)。”但是当我尝试编译时,出现了以上错误。 我不知道我做错了什么。
您在getCaloriesFromFat
的乘法中使用了double
文字9.0
。 因此, fatGrams
提升为double
,乘积为double
。 但是,您可以使用该方法返回int
。
您可以使用int
文字( 9
,无小数点),也可以让getCaloriesFromFat
返回double
而不是int
。 由于您只是乘以9
,因此我将使用int
文字9
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.