此问题在此处已有答案:
What does "possible lossy conversion" mean and how do I fix it?(1个答案)
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?(19个答案)
上个月关门了。
我是一个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;
}
}
}
非常感谢!
1条答案
按热度按时间gfttwv5a1#
这些编译错误发生在代码中的特定节点上;您的编辑器将用红色波浪下划线突出显示这些内容。如果您没有智能编辑器,请考虑购买一个(我推荐Eclipse或Intellij);但即使这样,javac也会打印相关的行,然后打印带有
^
符号的行,以指向该行的确切部分。这就是问题所在,然后读取错误,将其应用到错误所指向的节点。
例如,
incompatible types: possible lossy conversion from double to int
的意思就是:您正在获取double
类型的值,并试图将其分配给int
,这可能会导致信息丢失。一个
int
是一个介于正负20亿之间的整数。一个double
有一个小数部分。所以,如果Cost
是3,那么3 * 1.15
就是3.45
,然后......现在怎么办?TotalCost
不能是3.45
(它是一个整数;它只能是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 wasn't
2? In fact, had the code compiled, weird stuff would happen: What would
CeilingAreaeven be? Not
12 * 7; x wasn't 2, that line did not run. So, the idea that java doesn't allow
CeilingArea '存在于最接近的括号之外,那会有什么内在的意义呢?要解决这个问题,您的代码并不清晰,因此您需要考虑这意味着什么。例如,如果您希望
CeilingArea
保留在括号之外,请在括号之外声明它(就像您对Money
变量所做的那样)。调试器指的是可以很好地编译但不能完成你所期望的代码。修复编译器错误不是“调试”。因此,调试工具(如调试器)并不适用。没有简单的修复;学习java,这样你就能理解编译器的错误。这需要几周/几个月/几年,至少对我来说是这样的:)