【发布时间】:2020-10-19 03:21:13
【问题描述】:
我是 java 初学者,我已经编写了这段代码。当我尝试运行它时,它一直显示如下错误:
- 错误:不兼容的类型:从 double 到 int 的可能有损转换
- 错误:找不到符号
有人可以解释一下我作为初学者如何解决这个问题。
public class JavaJoeWeek{
public static void main (String[] args){
int x = 1;
int Money = 200;
int Twenty, Ten, Toonie, Loonie, Quarter, Dime, Nickel = 0;
while (x < 8){
System.out.println("Today is day " + x + " of the week");
if (x == 1) {
int Cost = 30;
int TotalCost = Cost * 1.15;
System.out.println("The cost of the shoes is $" + TotalCost);
Money = Money - TotalCost;
System.out.println("The remaining budget is $" + Money );
}
else{
System.out.println();
}
if (x == 2) {
int CeilingArea = 12 * 7;
System.out.println("The area of the celing is " + CeilingArea + " square meters");
System.out.println("The remaining budget is $" + Money );
}
else{
System.out.println();
}
if (x == 3) {
int PaintCost = CeilingArea * 1.13;
System.out.println("The paint cost $" + PaintCost);
Money = Money - PaintCost;
System.out.println("The remaining budget is $" + Money );
}
else{
System.out.println();
}
if (x == 4) {
int Gas = 36.40 / 0.45;
System.out.println("Java Joe filled up his tank with " + Gas + " liters of gas");
Money = Money - Gas;
System.out.println("The remaining budget is $" + Money );
}
else{
System.out.println();
}
if (x == 5) {
System.out.println("The remaining budget is $" + Money );
}
else{
System.out.println();
}
if (x == 6) {
Money = Money - 23;
System.out.println("The remaining budget is $" + Money );
}
else{
System.out.println();
}
if (x == 7) {
if (Money > 20) {
Money = Money - 20;
Twenty = Twenty + 1;
}
else{
System.out.println("Java Joe has " + Twenty + " twenty dollar bills");
}
if (Money > 10) {
Money = Money - 10;
Ten = Ten + 1;
}
else{
System.out.println("Java Joe has " + Ten + " ten dollar bills");
}
if (Money > 2) {
Money = Money - 2;
Toonie = Toonie + 1;
}
else{
System.out.println("Java Joe has " + Toonie + " toonies");
}
if (Money > 20) {
Money = Money - 20;
Loonie = Loonie + 1;
}
else{
System.out.println("Java Joe has " + Loonie + " loonies");
}
if (Money > 0.25) {
Money = Money - 0.25;
Quarter = Quarter + 1;
}
else{
System.out.println("Java Joe has " + Quarter + " quarters");
}
if (Money > 0.1) {
Money = Money - 0.1;
Dime = Dime + 1;
}
else{
System.out.println("Java Joe has " + Dime + " dimes");
}
if (Money > 0.05) {
Money = Money - 0.05;
Nickel = Nickel + 1;
}
else{
System.out.println("Java Joe has " + Nickel + " nickels");
}
System.out.println("There is $" + Money + " left after the change" );
}
else{
System.out.println();
}
x = x + 1;
}
}
}
非常感谢!
【问题讨论】:
-
嘿!你明白这些错误告诉你什么吗?为什么?您是否对错误进行过任何研究?这些都是非常微不足道的错误,相信我,只要你花一点精力研究和调试,你就可以自己解决它们。
-
请研究一下类型转换。你可以自己做