不知道如何修复:错误:不兼容的类型:从double到int的可能有损转换

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

Don't know how to fix: error: incompatible types: possible lossy conversion from double to int

问题

  1. import java.util.Scanner;
  2. import java.lang.Math;
  3. public class PaintEstimator {
  4. public static void main(String[] args) {
  5. Scanner scnr = new Scanner(System.in);
  6. double wallHeight;
  7. double wallWidth;
  8. double wallArea;
  9. System.out.println("Enter wall height (feet): ");
  10. wallHeight = scnr.nextDouble();
  11. System.out.println(wallHeight);
  12. wallWidth = scnr.nextDouble();
  13. System.out.println("Enter wall width (feet): ");
  14. System.out.println(wallWidth);
  15. wallArea = wallHeight * wallWidth;
  16. System.out.println("Wall area: " + (wallArea) + " square feet");
  17. double paintNeeded;
  18. int cansNeeded;
  19. final double squareFeetPerGallons;
  20. final double gallonsPerCan;
  21. squareFeetPerGallons = 350.0;
  22. gallonsPerCan = 1.0;
  23. System.out.print("Paint needed: ");
  24. paintNeeded = wallArea / squareFeetPerGallons;
  25. System.out.print(paintNeeded);
  26. System.out.println(" gallons");
  27. cansNeeded = (int) Math.ceil(paintNeeded / gallonsPerCan); // Fixed line 34
  28. System.out.print("Cans needed: ");
  29. System.out.print(cansNeeded);
  30. System.out.println(" can(s)");
  31. }
  32. }
英文:

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.

  1. import java.util.Scanner;
  2. import java.lang.Math;
  3. public class PaintEstimator {
  4. public static void main(String[] args) {
  5. Scanner scnr = new Scanner(System.in);
  6. double wallHeight;
  7. double wallWidth;
  8. double wallArea;
  9. System.out.println("Enter wall height (feet): ");
  10. wallHeight = scnr.nextDouble();
  11. System.out.println(wallHeight);
  12. wallWidth = scnr.nextDouble();
  13. System.out.println("Enter wall width (feet): ");
  14. System.out.println(wallWidth);
  15. wallArea = wallHeight * wallWidth;
  16. System.out.println("Wall area: " + (wallArea) + " square feet");
  17. double paintNeeded;
  18. int cansNeeded;
  19. final double squareFeetPerGallons;
  20. final double gallonsPerCan;
  21. squareFeetPerGallons = 350.0;
  22. gallonsPerCan = 1.0;
  23. System.out.print("Paint needed: ");
  24. paintNeeded = wallArea / squareFeetPerGallons;
  25. System.out.print(paintNeeded);
  26. System.out.println(" gallons");
  27. cansNeeded = paintNeeded / gallonsPerCan;
  28. System.out.print("Cans needed: ");
  29. System.out.print(cansNeeded);
  30. System.out.println(" can(s)");

}
}

答案1

得分: 2

整数没有小数点,不同于会有小数点的双精度数(doubles)。

你只能将整数赋值给整型变量(int),不能赋值给双精度数(double)等。

在你的情况下,你可能希望将 cansNeeded 定义为双精度数(double)(因为其他每个变量都是双精度数)。应该像这样:

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

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

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

  1. cansNeeded = (int) Math.ceil(paintNeeded / gallonsPerCan);

huangapple
  • 本文由 发表于 2020年9月12日 04:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63854236.html
匿名

发表评论

匿名网友

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

确定