调试错误:不兼容的类型:从 double 到 int 可能会有损失的转换

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

Debugging error: incompatible types: possible lossy conversion from double to int

问题

public class JavaJoeWeek{

    public static void main(String[] args){

        int x = 1;
        int Money = 200;
        int Twenty = 0, Ten = 0, Toonie = 0, Loonie = 0, Quarter = 0, Dime = 0, Nickel = 0; // Initialize each variable separately
        int CeilingArea = 12 * 7; // Declare CeilingArea outside the loop
        while (x < 8){
            System.out.println("Today is day " + x + " of the week");
            if (x == 1) {
                int Cost = 30;
                int TotalCost = Cost * 115 / 100; // Ensure TotalCost is of type int
                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) {
                System.out.println("The area of the ceiling is " + CeilingArea + " square meters");
                System.out.println("The remaining budget is $" + Money);
            }
            else{
                System.out.println();
            }
            if (x == 3) {
                int PaintCost = CeilingArea * 113 / 100; // Ensure PaintCost is of type int
                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 = (int) (36.40 / 0.45); // Cast the result of the division to int
                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;
        }
    }
}
英文:

I'm a beginner with java, and I have written this code. When I attempt to run it, it keeps displaying errors like:

  • error: incompatible types: possible lossy conversion from double to int
  • error: cannot find symbol

Could someone explain how I might un-bug this as a beginner.

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 &lt; 8){
System.out.println(&quot;Today is day &quot; + x + &quot; of the week&quot;);
if (x == 1) {
int Cost = 30;
int TotalCost = Cost * 1.15;
System.out.println(&quot;The cost of the shoes is $&quot; + TotalCost);
Money = Money - TotalCost;
System.out.println(&quot;The remaining budget is $&quot; + Money );
}
else{
System.out.println();
}
if (x == 2) {
int CeilingArea = 12 * 7;
System.out.println(&quot;The area of the celing is &quot; + CeilingArea + &quot; square meters&quot;);
System.out.println(&quot;The remaining budget is $&quot; + Money );
}
else{
System.out.println();
}
if (x == 3) {
int PaintCost = CeilingArea * 1.13;
System.out.println(&quot;The paint cost $&quot; + PaintCost);
Money = Money - PaintCost;
System.out.println(&quot;The remaining budget is $&quot; + Money );
}
else{
System.out.println();
}
if (x == 4) {
int Gas = 36.40 / 0.45;
System.out.println(&quot;Java Joe filled up his tank with &quot; + Gas + &quot; liters of gas&quot;);
Money = Money - Gas;
System.out.println(&quot;The remaining budget is $&quot; + Money );
}
else{
System.out.println();
}
if (x == 5) {
System.out.println(&quot;The remaining budget is $&quot; + Money );
}
else{
System.out.println();
}
if (x == 6) {
Money = Money - 23;
System.out.println(&quot;The remaining budget is $&quot; + Money );
}
else{
System.out.println();
}
if (x == 7) {
if (Money &gt; 20) {
Money = Money - 20;
Twenty =  Twenty + 1;
}
else{
System.out.println(&quot;Java Joe has &quot; + Twenty + &quot; twenty dollar bills&quot;);
}
if (Money &gt; 10) {
Money = Money - 10;
Ten =  Ten + 1;
}
else{
System.out.println(&quot;Java Joe has &quot; + Ten + &quot; ten dollar bills&quot;);
}	
if (Money &gt; 2) {
Money = Money - 2;
Toonie =  Toonie + 1;
}
else{
System.out.println(&quot;Java Joe has &quot; + Toonie + &quot; toonies&quot;);
}
if (Money &gt; 20) {
Money = Money - 20;
Loonie =  Loonie + 1;
}
else{
System.out.println(&quot;Java Joe has &quot; + Loonie + &quot; loonies&quot;);
}
if (Money &gt; 0.25) {
Money = Money - 0.25;
Quarter =  Quarter + 1;
}
else{
System.out.println(&quot;Java Joe has &quot; + Quarter + &quot; quarters&quot;);
}
if (Money &gt; 0.1) {
Money = Money - 0.1;
Dime =  Dime + 1;
}
else{
System.out.println(&quot;Java Joe has &quot; + Dime + &quot; dimes&quot;);
}
if (Money &gt; 0.05) {
Money = Money - 0.05;
Nickel =  Nickel + 1;
}
else{
System.out.println(&quot;Java Joe has &quot; + Nickel + &quot; nickels&quot;);
}
System.out.println(&quot;There is $&quot; + Money + &quot; left after the change&quot; );
}
else{
System.out.println();
}
x = x + 1;
}
}
}

Thank you so much!

答案1

得分: 1

以下是翻译好的部分:

这些编译错误发生在代码中特定的节点上;您的编辑器会用红色的波浪线对这些错误进行标记。如果您没有智能编辑器,强烈考虑获取一个(我推荐Eclipse或Intellij);但即便如此,javac会打印相关的行,然后在该行下方打印带有 ^ 符号的行,以指示确切的错误部分。

问题就出在这里。然后只需要... 阅读错误信息,将其应用于错误指向的节点。

例如,incompatible types: possible lossy conversion from double to int 的意思就是它字面上所说的:您正在将一个类型为 double 的值尝试分配给一个 int,这可能会导致信息丢失。

int 是大约在负2十亿到正2十亿之间的整数。而 double 有一个小数部分。所以,如果 Cost 是3,那么 3 * 1.15 就是 3.45,然后呢?TotalCost 不能是 3.45(它是一个int;它只能是3或4)。

您可以通过告诉编译器您希望它执行什么操作来修复这个问题。例如,int TotalCost = (int) (Cost * 1.15); 是在向编译器请求:请明确地将这个double转换为int,并且Java语言规范随后指出,这种特定的策略将去掉小数部分(所以,如果是正数则向下舍入,如果是负数则向上舍入)。

下一个错误涉及到 'Cannot find symbol'。您使用了一个符号(即标识符或代码中引用其他内容的单词),而编译器不知道您在说什么。

例如,在 int PaintCost = CeilingArea * 1.13 中的 CeilingArea 就不是一个已知的符号。听起来有点奇怪,对吧?CeilingArea 就在您的代码中,大约在上面6行。但是,错误信息并没有欺骗您,所以您需要进行一些研究,或者停顿片刻思考一下。

在Java中,变量在最近的花括号({})之外是不存在的。关闭 if (x == 2) 块的花括号也意味着您在其中声明的 CeilingArea 变量将不复存在。这是合乎逻辑的;如果 x 不是 2 呢?实际上,如果代码编译通过,会发生一些奇怪的事情:CeilingArea 会是什么呢?不是 12 * 7;因为 x 不是2,那一行代码根本没有运行。所以,Java不允许 CeilingArea 在最近的花括号之外存在的想法是有道理的。

要修复这个问题,嗯,您的代码不太清晰,所以您需要考虑这意味着什么。如果您希望 CeilingArea 在花括号之外仍然存在,可以在花括号之外声明它(就像您在 Money 变量的情况下所做的那样),例如。

调试器是指代码在编译时没有问题,但不会按您预期的方式工作的情况。修复编译错误并不等同于“调试”。因此,用于调试的工具(例如调试器)并不适用。没有简单的解决办法;学习Java,以便您能够理解编译错误。这需要几周/几个月/几年的时间,至少对我来说是这样 调试错误:不兼容的类型:从 double 到 int 可能会有损失的转换

英文:

These compilation errors occur on specific nodes in your code; your editor will highlight these with a red wavy underline. If you don't have a smart editor, strongly consider getting one (I recommend Eclipse or Intellij); but even then, javac will print the relevant line and then a line with ^ symbols to point at the exact part of the line.

That's where the problem lies. Then just.. read the error, apply it to the node that the error is pointing at.

For example, incompatible types: possible lossy conversion from double to int means just what it says: You're taking a value of type double, and trying to assign it to an int, and this might lead to lost info.

An int is a whole number between about minus and plus 2 billion. A double has a fractional part. So, if Cost is 3, then 3 * 1.15 is 3.45, and.. now what? TotalCost can't be 3.45 (it is an int; it can only be 3 or 4).

You can fix this by telling the compiler what you want it to do. For example, int TotalCost = (int) (Cost * 1.15); is asking the compiler: Please convert this double to an int explicitly, and the java language specification then states that this particular strategy will lop off the decimal bits (so, round down if positive, up if negative).

The next error concerns 'Cannot find symbol'. You're using a symbol (that'd be an identifier or some other word that refers to something else in your code) and the compiler has no idea what you're talking about.

For example, CeilingArea in int PaintCost = CeilingArea * 1.13 is not a known symbol there. Seems weird, right? CeilingArea is right there in your code, about 6 lines up. But, the error isn't lying to you, so you'd do some research, or just think about it for a second.

In java, a variable does not exist outside the nearest brackets (the {}). The bracket that closes the if (x == 2) block also means the CeilingArea variable you declared inside it ceases to exist. This is logical; what if x wasn&#39;t 2? In fact, had the code compiled, weird stuff would happen: What would CeilingAreaeven be? Not12 * 7; x wasn&#39;t 2, that line did not run. So, the idea that java doesn&#39;t allow CeilingArea` to exist outside of the nearest brackets makes inherent sense.

To fix that, well, your code isn't clear, so you need to think about what this means. if you want CeilingArea to survive outside the brackets, declare it outside of them (the way you did with the Money variable), for example.

Debugger refers to code that compiles fine but doesn't do what you expected it to. Fixing compiler errors isn't 'debugging'. Hence, tools to debug (such as debuggers) don't apply. There is no easy fix; learn java so that you understand the compiler errors. Which takes some weeks / months / years, at least, it did for me 调试错误:不兼容的类型:从 double 到 int 可能会有损失的转换

huangapple
  • 本文由 发表于 2020年6月29日 09:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/62630052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定