回答編集履歴
4
追記
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Math.roundの戻り値はlong型ですが、
|
1
|
+
[Math.round(double)](https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Math.html#round-double-)の戻り値はlong型ですが、
|
2
2
|
longよりintの方が表現力が低いので、暗黙のキャストは認められていないのです。
|
3
3
|
|
4
4
|
戻り値を明示的にキャストするのが正解です。`(int)Math.round(...)`みたいに。
|
3
修正
answer
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
Math.roundの戻り値は
|
1
|
+
Math.roundの戻り値はlong型ですが、
|
2
|
-
|
2
|
+
longよりintの方が表現力が低いので、暗黙のキャストは認められていないのです。
|
3
3
|
|
4
4
|
戻り値を明示的にキャストするのが正解です。`(int)Math.round(...)`みたいに。
|
5
5
|
|
@@ -8,18 +8,18 @@
|
|
8
8
|
```Java
|
9
9
|
class Main {
|
10
10
|
static int func() {
|
11
|
-
return
|
11
|
+
return 0L;
|
12
12
|
}
|
13
13
|
|
14
14
|
public static void main(String[] args) {
|
15
|
+
|
15
16
|
}
|
16
17
|
}
|
17
|
-
|
18
18
|
```
|
19
19
|
|
20
|
-
**エラーメッセージ** [Wandbox](https://wandbox.org/permlink/
|
20
|
+
**エラーメッセージ** [Wandbox](https://wandbox.org/permlink/7VdtrjLpw6C3FrLq)
|
21
21
|
```plain
|
22
|
-
prog.java:3: error: incompatible types: possible lossy conversion from
|
22
|
+
prog.java:3: error: incompatible types: possible lossy conversion from long to int
|
23
|
-
return
|
23
|
+
return 0L;
|
24
24
|
^
|
25
25
|
```
|
2
修正
answer
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Math.roundの戻り値はdouble型ですが、
|
2
2
|
doubleよりintの方が表現力が低いので、暗黙のキャストは認められていないのです。
|
3
3
|
|
4
|
-
|
4
|
+
戻り値を明示的にキャストするのが正解です。`(int)Math.round(...)`みたいに。
|
5
5
|
|
6
6
|
同様のエラーを起こすコード
|
7
7
|
---
|
1
追記
answer
CHANGED
@@ -1,4 +1,25 @@
|
|
1
1
|
Math.roundの戻り値はdouble型ですが、
|
2
2
|
doubleよりintの方が表現力が低いので、暗黙のキャストは認められていないのです。
|
3
3
|
|
4
|
-
質問者さんのように明示的にキャストするのが正解です。
|
4
|
+
質問者さんのように明示的にキャストするのが正解です。
|
5
|
+
|
6
|
+
同様のエラーを起こすコード
|
7
|
+
---
|
8
|
+
```Java
|
9
|
+
class Main {
|
10
|
+
static int func() {
|
11
|
+
return 0.0;
|
12
|
+
}
|
13
|
+
|
14
|
+
public static void main(String[] args) {
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
```
|
19
|
+
|
20
|
+
**エラーメッセージ** [Wandbox](https://wandbox.org/permlink/OTuAnyMIEpC4GCJ6)
|
21
|
+
```plain
|
22
|
+
prog.java:3: error: incompatible types: possible lossy conversion from double to int
|
23
|
+
return 0.0;
|
24
|
+
^
|
25
|
+
```
|