【发布时间】:2021-04-26 08:03:42
【问题描述】:
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
【问题讨论】:
-
欢迎来到 Stack Overflow。 在哪里你得到错误?你能把你的问题减少到minimal reproducible example吗?我怀疑您只需要一个变量声明,例如声明一个
int变量,但为其分配一个非整数初始值。您对int类型有什么了解,您希望为这样的变量分配一个非整数值做什么? (顺便说一句,现在是学习 Java 命名约定的好时机。) -
"//变量声明" 不要这样声明变量。尽可能将它们声明在靠近您需要它们的位置。
标签: java integer double type-mismatch