"Type mismatch: cannot convert from double to int Error"

"Type mismatch: cannot convert from double to int Error"

import java.util.Scanner; 

public class PaintCostCalculator {
   public static void main( String args[] )
   {
    try (Scanner input = new Scanner(System.in)) {

    //variable declarations
    int NoOfRooms;
    int RoomCounter;
    int choice;
    int Area = 0;
    int AreaSum = 0;
    int TotalLSCost;
    int TotalSGCost;
    int TotalMatCost;

    
    //constants declarations
    final int PaintCoverage = 16;
    final int LowSheenCost = 17.6;
    final int SemiGlossCost = 20;
    final int MatteCost = 14.3;
    
    //code
    System.out.print("Please enter the number of rooms to be painted: ");
    NoOfRooms = input.nextInt();
    for(RoomCounter = 0; RoomCounter < NoOfRooms; RoomCounter ++) {
        System.out.printf("\nEnter the area of room %d in m^2.: ", RoomCounter + 1);
        Area = input.nextInt();
        AreaSum = AreaSum + Area;
    }
    System.out.println("\nPlease choose one of the following paint options: \n1. Low Sheen(.60/L)\n2. Semi Gloss(/L)\n3. Matte(.30/L)");
    choice = input.nextInt();
    switch (choice)
    {
        case 1: 
                System.out.print("You have chosen Low Sheen\n");
                TotalLSCost = (AreaSum / PaintCoverage) * LowSheenCost;
                System.out.printf("To paint a total area of %dm^2 with Low Sheen paint it would cost a total of %d", AreaSum, TotalLSCost);
                break;
        case 2: 
                System.out.print("You have chosen Semi Gloss\n");
                TotalSGCost = (AreaSum / PaintCoverage) * SemiGlossCost;
                System.out.printf("To paint a total area of %dm^2 with Semi Gloss paint it would cost a total of %d", AreaSum, TotalSGCost);
                break;
        case 3: 
                System.out.print("You have chosen Matte\n");
                TotalMatCost = (AreaSum / PaintCoverage) * MatteCost;
                System.out.printf("To paint a total area of %dm^2 with Matte paint it would cost a total of %d", AreaSum, TotalMatCost);
                break;
    }
    }
   }
}

我仍处于 Java 的早期学习阶段,这是我的第一语言,正在尝试练习程序任务。简单的程序要求用户提供房间数量、每个房间的面积,然后提供 3 种油漆选项的选择,它将计算要油漆的总面积和所需油漆的价格。我收到错误:

Type mismatch: cannot convert from double to int

final int LowSheenCost = 17.6;

17.6 是一个双重文字:它有一个整数部分和一个小数部分。您不能将其放入需要整数的变量中。

改为:

final double LowSheenCost = 17.6;

(其他人也一样)


一个小问题是:

final int PaintCoverage = 16;

有效;但它以后可能会给你带来意想不到的结果,因为这样:

(AreaSum / PaintCoverage)

TotalLSCost = (AreaSum / PaintCoverage) * LowSheenCost;

整数除法,即除法只会得到一个整数:如果|AreaSum| < 16,它的值为零;如果 16 <= AreaSum < 32,它的值将是 1 等等

因此,如果您打算进行小数除法,也将 PaintCoverage 设为 double 而不是 int

您必须更改“TotalLSCost;TotalSGCost;TotalMatCost;”的数据类型加倍。 因为在乘法和除法中,输出值有可能有小数位,而 int 不能有小数。

如果您想要 int 数据类型本身,请尝试在这些行上进行显式转换

    TotalLSCost = (int) (AreaSum / PaintCoverage) * LowSheenCost;
    TotalSGCost = (int) (AreaSum / PaintCoverage) * SemiGlossCost;
    TotalMatCost = (int) (AreaSum / PaintCoverage) * MatteCost;