double wang = 3 / 2;
Log.v("TEST", "Wang: " + Double.toString(wang));
Run Code Online (Sandbox Code Playgroud)
Logcat输出......
07-04 09:01:03.908: VERBOSE/TEST(28432): Wang: 1.0
Run Code Online (Sandbox Code Playgroud)
我确信这有一个明显的答案,可能我只是整夜编码累了,但这让我很难过.
Tom*_*han 13
在许多语言中,Java就是其中之一,在表达式中编写数字的方式决定了它获得的类型.在Java中,一些常见的数字类型表现得像这样1:
// In these cases the specs are obviously redundant, since all values will be
// cast correctly anyway, but it was the easiest way to show how to get to the
// different data types :P
int i = 1;
long l = 1L;
float f = 1.0f; // I believe the f and d for float and double are optional, but
double d = 1.0d; // I wouldn't bet on what the default is if they're omitted...
Run Code Online (Sandbox Code Playgroud)
因此,当你宣布时3 / 2,你真的在说(the integer 3) / (the integer 2).Java执行除法,并查找结果1(即the integer 1...),因为这是将3和2除以整数的结果.最后,the integer 1转换the double 1.0d为存储在变量中的内容.
要解决这个问题,你应该(正如许多其他人所建议的那样)来计算商数
(the double 3) / (the double 2)
Run Code Online (Sandbox Code Playgroud)
或者,在Java语法中,
double wang = 3.0 / 2.0;
Run Code Online (Sandbox Code Playgroud)
1来源:Oracle 的Java教程
| 归档时间: |
|
| 查看次数: |
24406 次 |
| 最近记录: |