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

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

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

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:

确定