回答編集履歴

4

追記

2018/02/22 08:04

投稿

LouiS0616
LouiS0616

スコア35660

test 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
 
3
3
  longよりintの方が表現力が低いので、暗黙のキャストは認められていないのです。
4
4
 

3

修正

2018/02/22 08:04

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -1,6 +1,6 @@
1
- Math.roundの戻り値はdouble型ですが、
1
+ Math.roundの戻り値はlong型ですが、
2
2
 
3
- doubleよりintの方が表現力が低いので、暗黙のキャストは認められていないのです。
3
+ longよりintの方が表現力が低いので、暗黙のキャストは認められていないのです。
4
4
 
5
5
 
6
6
 
@@ -18,7 +18,7 @@
18
18
 
19
19
  static int func() {
20
20
 
21
- return 0.0;
21
+ return 0L;
22
22
 
23
23
  }
24
24
 
@@ -26,23 +26,23 @@
26
26
 
27
27
  public static void main(String[] args) {
28
28
 
29
+
30
+
29
31
  }
30
32
 
31
33
  }
32
-
33
-
34
34
 
35
35
  ```
36
36
 
37
37
 
38
38
 
39
- **エラーメッセージ** [Wandbox](https://wandbox.org/permlink/OTuAnyMIEpC4GCJ6)
39
+ **エラーメッセージ** [Wandbox](https://wandbox.org/permlink/7VdtrjLpw6C3FrLq)
40
40
 
41
41
  ```plain
42
42
 
43
- prog.java:3: error: incompatible types: possible lossy conversion from double to int
43
+ prog.java:3: error: incompatible types: possible lossy conversion from long to int
44
44
 
45
- return 0.0;
45
+ return 0L;
46
46
 
47
47
  ^
48
48
 

2

修正

2018/02/22 08:01

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- 質問者さんのように明示的にキャストするのが正解です。
7
+ 戻り値を明示的にキャストするのが正解です。`(int)Math.round(...)`みたいに。
8
8
 
9
9
 
10
10
 

1

追記

2018/02/22 07:59

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -5,3 +5,45 @@
5
5
 
6
6
 
7
7
  質問者さんのように明示的にキャストするのが正解です。
8
+
9
+
10
+
11
+ 同様のエラーを起こすコード
12
+
13
+ ---
14
+
15
+ ```Java
16
+
17
+ class Main {
18
+
19
+ static int func() {
20
+
21
+ return 0.0;
22
+
23
+ }
24
+
25
+
26
+
27
+ public static void main(String[] args) {
28
+
29
+ }
30
+
31
+ }
32
+
33
+
34
+
35
+ ```
36
+
37
+
38
+
39
+ **エラーメッセージ** [Wandbox](https://wandbox.org/permlink/OTuAnyMIEpC4GCJ6)
40
+
41
+ ```plain
42
+
43
+ prog.java:3: error: incompatible types: possible lossy conversion from double to int
44
+
45
+ return 0.0;
46
+
47
+ ^
48
+
49
+ ```