英文:
Don't know how to fix: error: incompatible types: possible lossy conversion from double to int
问题
import java.util.Scanner;
import java.lang.Math;
public class PaintEstimator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight;
double wallWidth;
double wallArea;
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
System.out.println(wallHeight);
wallWidth = scnr.nextDouble();
System.out.println("Enter wall width (feet): ");
System.out.println(wallWidth);
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + (wallArea) + " square feet");
double paintNeeded;
int cansNeeded;
final double squareFeetPerGallons;
final double gallonsPerCan;
squareFeetPerGallons = 350.0;
gallonsPerCan = 1.0;
System.out.print("Paint needed: ");
paintNeeded = wallArea / squareFeetPerGallons;
System.out.print(paintNeeded);
System.out.println(" gallons");
cansNeeded = (int) Math.ceil(paintNeeded / gallonsPerCan); // Fixed line 34
System.out.print("Cans needed: ");
System.out.print(cansNeeded);
System.out.println(" can(s)");
}
}
英文:
I can't figure out what is wrong on line 34 and I need help fixing it. I don't know if I incorrectly input something or if I need to convert something.
import java.util.Scanner;
import java.lang.Math;
public class PaintEstimator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight;
double wallWidth;
double wallArea;
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
System.out.println(wallHeight);
wallWidth = scnr.nextDouble();
System.out.println("Enter wall width (feet): ");
System.out.println(wallWidth);
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + (wallArea) + " square feet");
double paintNeeded;
int cansNeeded;
final double squareFeetPerGallons;
final double gallonsPerCan;
squareFeetPerGallons = 350.0;
gallonsPerCan = 1.0;
System.out.print("Paint needed: ");
paintNeeded = wallArea / squareFeetPerGallons;
System.out.print(paintNeeded);
System.out.println(" gallons");
cansNeeded = paintNeeded / gallonsPerCan;
System.out.print("Cans needed: ");
System.out.print(cansNeeded);
System.out.println(" can(s)");
}
}
答案1
得分: 2
整数没有小数点,不同于会有小数点的双精度数(doubles)。
你只能将整数赋值给整型变量(int),不能赋值给双精度数(double)等。
在你的情况下,你可能希望将 cansNeeded 定义为双精度数(double)(因为其他每个变量都是双精度数)。应该像这样:
double cansNeeded = paintNeeded / gallonsPerCan
然后你可以根据需要处理结果(向上取整、向下取整等)。这样更精确。
要向上取整,使用 Math.ceil(cansNeeded)
。要向下取整,使用 Math.floor(cansNeeded)
。
英文:
Integers don't have decimal points, unlike doubles, which do.
You can only assign an integer to an int, not a double etc,.
In your case, you probably want to define cansNeeded as a double (every single other variable is a double). It should look like this:
double cansNeeded = paintNeeded / gallonsPerCan
Then you can deal with the result accordingly (round up, round down etc,.). This is more precise.
To round up, use Math.ceil(cansNeeded)
. To round down, use Math.floor(cansNeeded)
.
答案2
得分: 1
将双精度数相除的结果是一个双精度数,无法赋值给整型。在您的情况下,您可能想使用 Math.ceil
,然后转换为 int
。
cansNeeded = (int) Math.ceil(paintNeeded / gallonsPerCan);
英文:
The result of dividing doubles is a double, which can't be assigned to an int. In your case, you probably want to use Math.ceil
and then cast to int
.
cansNeeded = (int) Math.ceil(paintNeeded / gallonsPerCan);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论