簡體   English   中英

“類型不匹配:無法從 double 轉換為 int 錯誤”

[英]"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($17.60/L)\n2. Semi Gloss($20/L)\n3. Matte($14.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 種油漆選項的選擇,它將計算要油漆的總面積和所需的油漆價格。 我收到錯誤:

類型不匹配:無法從 double 轉換為 int

final int LowSheenCost = 17.6;

17.6是一個雙重文字:它有一個整數部分和一個小數部分。 您不能將其放入期望整數的變量中。

反而:

final double LowSheenCost = 17.6;

(對其他人也是如此)。


一個小問題是:

final int PaintCoverage = 16;

有效 但它可能會在以后給你意想不到的結果,因為這樣:

(AreaSum / PaintCoverage)

TotalLSCost = (AreaSum / PaintCoverage) * LowSheenCost;

integer 除法,即除法只能得到整數結果: if |AreaSum| < 16 |AreaSum| < 16 ,其值將為零; 如果16 <= AreaSum < 32 ,則其值為 1 等。

因此,如果您打算進行小數除法, PaintCoverage double而不是int

您必須更改“TotalLSCost; TotalSGCost; TotalMatCost;”的數據類型加倍。 因為在乘法和除法中,output 值有可能有小數位,而 int 不能有小數位。

如果您想要 int 數據類型本身,請嘗試在這些行上進行顯式轉換

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM