Java 从 double 到 int 的可能有损转换并且找不到符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34324407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible lossy conversion from double to int and cannot find symbol
提问by McDodger
I'm getting an error in my program saying:
我在我的程序中收到一条错误消息:
Lyrics.java:11: error: cannot find symbol
Cube(b); ^symbol: method Cube(int)
location: class Lyrics
Lyrics.java:15: error: incompatible types: possible lossy conversion from double to int
return Math.pow (b, 3); ^2 errors
Lyrics.java:11: 错误:找不到符号
Cube(b); ^符号:方法 Cube(int)
地点:课堂歌词
Lyrics.java:15: 错误:不兼容的类型:从 double 到 int 的可能有损转换
return Math.pow (b, 3); ^2 错误
I looked around on the website and it turns out other people also had this issue but i looked at the comments and i still dont understand the problem, theres no double in my code why would it say this. And i also dont undestand the error saying cannot find symbol. Please help ive really been stuck on this for a while.
我在网站上环顾四周,发现其他人也有这个问题,但我查看了评论,但我仍然不明白问题所在,我的代码中没有重复为什么会这样说。而且我也不理解错误说找不到符号。请帮助我真的坚持了一段时间。
import static java.lang.Math.pow;
import java.util.Scanner;
public class Lyrics
{
public static void main(String []args)
{
int b;
Scanner scan = new Scanner(System.in);
System.out.println ("Enter a number: ");
b = scan.nextInt();
Cube(b);
}
public static int cube (int b)
{
return Math.pow (b, 3);
}
}
采纳答案by Stephen C
The first problem is a simple typo. Java is case sensitive, so cubeand Cubemean different things. Solution: 1) be consistent, and 2) use names starting with lowercase letters for method names ... as per the Java style guides.
第一个问题是一个简单的错字。Java 区分大小写,所以cube和Cube意味着不同的东西。解决方案:1) 保持一致,以及 2) 使用以小写字母开头的名称作为方法名称......按照 Java 风格指南。
The second problem is due to the method signature for Math.pow; see the javadoc. It returns the result as a double. You then attempte to return the doubleas an int, and thatis a lossy conversion.
第二个问题是由于Math.pow;的方法签名造成的。请参阅javadoc。它将结果作为double. 然后您尝试将 返回double为int,这是一个有损转换。
The solutions include:
解决方案包括:
return b * b * b; // i.e. don't use `pow`.
or
或者
return (int) Math.pow(b, 3);
The second one directly addresses your compilation error by castingthe doublereturn to an int. This is the way you tell the compiler that the lossy conversion is actually OK.
第二个通过直接解决您的编译错误铸造的double回报的int。这是您告诉编译器有损转换实际上没问题的方式。
The lossy conversion error message you are seeing refers to that fact for large enough values of b, the result of Math.pow(b, 3)will be too large to be represented as an int1. With the type cast in place, Java will convert a floating point number that is "too large" into Integer.MAX_VALUE.
您看到的有损转换错误消息是指对于足够大的 值b, 的结果Math.pow(b, 3)将太大而无法表示为int1。类型转换到位后,Java 会将“太大”的浮点数转换为Integer.MAX_VALUE.
The first solution is faster and simpler code, but if bis too large, the calculations will silently overflow and you will get a nonsense answer.
第一个解决方案是更快更简单的代码,但如果b太大,计算将无声无息地溢出,你会得到一个无意义的答案。
UPDATE- If you wanted overflow to always be treated as an error then:
更新- 如果您希望始终将溢出视为错误,则:
return Math.multiplyExact(b, Math.multiplyExact(b, b));
or
或者
return Math.toIntExact((long) Math.pow(b, 3));
or variations on the above.
或上述变化。
1 - Actually, the compiler doesn't know about the semantics of pow, so it doesn't know about "... for large enough values of b...". But it does know that if the method call didreturn a large enough value, then the conversion wouldbe lossy. That's why the message says "possiblelossy conversion".
1 - 实际上,编译器不知道 的语义pow,因此它不知道"... 对于...的足够大的值b"。但它确实知道如果方法调用确实返回了足够大的值,那么转换将是有损的。这就是为什么消息说“可能有损转换”的原因。
回答by Jim Garrison
Two problems:
两个问题:
- Letter case is important:
Cubeis not the same ascube. - Your function
cube(int)returns an integer, but the output ofMath.powis adouble.
- 字母大小写很重要:
Cube与cube. - 您的函数
cube(int)返回一个整数,但输出Math.pow是一个double.
You should probably change cube()to return a double.
您可能应该更改cube()以返回一个double.
Then you have more code to write in the mainmethod to use the results of calling cube.
然后您需要在main方法中编写更多代码来使用调用的结果cube。

