回答編集履歴

6

コード修正

2020/02/01 08:37

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -34,19 +34,19 @@
34
34
 
35
35
  public static ColorThing from(Color color) {
36
36
 
37
- if (in_range(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
37
+ if (inRange(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
38
38
 
39
39
  return yellow;
40
40
 
41
- } else if (in_range(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
41
+ } else if (inRange(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
42
42
 
43
43
  return red;
44
44
 
45
- } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
45
+ } else if (inRange(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
46
46
 
47
47
  return green;
48
48
 
49
- } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
49
+ } else if (inRange(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
50
50
 
51
51
  return blue;
52
52
 
@@ -60,7 +60,7 @@
60
60
 
61
61
 
62
62
 
63
- private static boolean in_range(Color color,
63
+ private static boolean inRange(Color color,
64
64
 
65
65
  double redMin, double redOver,
66
66
 
@@ -68,17 +68,17 @@
68
68
 
69
69
  double blueMin, double blueOver) {
70
70
 
71
- return in_range(color.red, redMin, redOver)
71
+ return inRange(color.red, redMin, redOver)
72
72
 
73
- && in_range(color.green, greenMin, greenOver)
73
+ && inRange(color.green, greenMin, greenOver)
74
74
 
75
- && in_range(color.blue, blueMin, blueOver);
75
+ && inRange(color.blue, blueMin, blueOver);
76
76
 
77
77
  }
78
78
 
79
79
 
80
80
 
81
- private static boolean in_range(double color, double min, double over) {
81
+ private static boolean inRange(double color, double min, double over) {
82
82
 
83
83
  return min <= color && color < over;
84
84
 

5

コメント削除

2020/02/01 08:37

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -36,31 +36,21 @@
36
36
 
37
37
  if (in_range(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
38
38
 
39
- // yellow actions
40
-
41
39
  return yellow;
42
40
 
43
41
  } else if (in_range(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
44
-
45
- // red actions
46
42
 
47
43
  return red;
48
44
 
49
45
  } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
50
46
 
51
- // green actions
52
-
53
47
  return green;
54
48
 
55
49
  } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
56
50
 
57
- // blue actions
58
-
59
51
  return blue;
60
52
 
61
53
  } else {
62
-
63
- // actions when unreadable
64
54
 
65
55
  return cantRead;
66
56
 

4

コード変更

2020/02/01 08:33

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -29,32 +29,6 @@
29
29
  enum ColorThing {
30
30
 
31
31
  yellow, red, green, blue, cantRead, outOfRange;
32
-
33
-
34
-
35
- public static boolean in_range(double color, double min, double over) {
36
-
37
- return min <= color && color < over;
38
-
39
- }
40
-
41
-
42
-
43
- public static boolean in_range(Color color,
44
-
45
- double redMin, double redOver,
46
-
47
- double greenMin, double greenOver,
48
-
49
- double blueMin, double blueOver) {
50
-
51
- return in_range(color.red, redMin, redOver)
52
-
53
- && in_range(color.green, greenMin, greenOver)
54
-
55
- && in_range(color.blue, blueMin, blueOver);
56
-
57
- }
58
32
 
59
33
 
60
34
 
@@ -94,6 +68,32 @@
94
68
 
95
69
  }
96
70
 
71
+
72
+
73
+ private static boolean in_range(Color color,
74
+
75
+ double redMin, double redOver,
76
+
77
+ double greenMin, double greenOver,
78
+
79
+ double blueMin, double blueOver) {
80
+
81
+ return in_range(color.red, redMin, redOver)
82
+
83
+ && in_range(color.green, greenMin, greenOver)
84
+
85
+ && in_range(color.blue, blueMin, blueOver);
86
+
87
+ }
88
+
89
+
90
+
91
+ private static boolean in_range(double color, double min, double over) {
92
+
93
+ return min <= color && color < over;
94
+
95
+ }
96
+
97
97
  };
98
98
 
99
99
 

3

文言訂正

2020/02/01 08:31

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,6 +1,6 @@
1
- 比較メソッドにしてはいかがですか?
1
+ メソッドにしてはいかがですか?
2
2
 
3
- detectColor クラスにメソッド追加できるのが一番でょうけど、とりあえずローカルメソッドで
3
+ ColorThingのメソッドてみました
4
4
 
5
5
 
6
6
 
@@ -115,7 +115,3 @@
115
115
  }
116
116
 
117
117
  ```
118
-
119
-
120
-
121
- さらに、色の範囲と対応enumのリストにしてループで回すのもいいかもしれません。

2

コード修正

2020/02/01 08:25

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -64,31 +64,31 @@
64
64
 
65
65
  // yellow actions
66
66
 
67
- return ColorThing.yellow;
67
+ return yellow;
68
68
 
69
69
  } else if (in_range(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
70
70
 
71
71
  // red actions
72
72
 
73
- return ColorThing.red;
73
+ return red;
74
74
 
75
75
  } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
76
76
 
77
77
  // green actions
78
78
 
79
- return ColorThing.green;
79
+ return green;
80
80
 
81
81
  } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
82
82
 
83
83
  // blue actions
84
84
 
85
- return ColorThing.blue;
85
+ return blue;
86
86
 
87
87
  } else {
88
88
 
89
89
  // actions when unreadable
90
90
 
91
- return ColorThing.cantRead;
91
+ return cantRead;
92
92
 
93
93
  }
94
94
 

1

コード修正

2020/02/01 08:21

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -6,61 +6,113 @@
6
6
 
7
7
  ```java
8
8
 
9
- public static boolean in_range(Color color,
9
+ class Color {
10
10
 
11
- float redMin, float redOver,
11
+ double red, green, blue;
12
12
 
13
- float greenMin, float greemOver,
14
13
 
15
- float blueMin, float blueOver) {
16
14
 
17
- return redMin <= color.red && color.red < redOver
15
+ Color(double red, double green, double blue) {
18
16
 
19
- && greenMin <= color.green && color.green < greenOver
17
+ this.red = red;
20
18
 
19
+ this.green = green;
20
+
21
- && blueMin <= color.blue && color.blue < blueOver;
21
+ this.blue = blue;
22
22
 
23
23
  }
24
24
 
25
- ```
25
+ }
26
26
 
27
27
 
28
28
 
29
- ```java
29
+ enum ColorThing {
30
30
 
31
- ColorThing colorOutput = ColorThing.cantRead;
31
+ yellow, red, green, blue, cantRead, outOfRange;
32
32
 
33
+
34
+
35
+ public static boolean in_range(double color, double min, double over) {
36
+
37
+ return min <= color && color < over;
38
+
39
+ }
40
+
41
+
42
+
43
+ public static boolean in_range(Color color,
44
+
45
+ double redMin, double redOver,
46
+
47
+ double greenMin, double greenOver,
48
+
49
+ double blueMin, double blueOver) {
50
+
51
+ return in_range(color.red, redMin, redOver)
52
+
53
+ && in_range(color.green, greenMin, greenOver)
54
+
55
+ && in_range(color.blue, blueMin, blueOver);
56
+
57
+ }
58
+
59
+
60
+
61
+ public static ColorThing from(Color color) {
62
+
33
- if (in_range(detectedColor, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
63
+ if (in_range(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
34
64
 
35
65
  // yellow actions
36
66
 
37
- colorOutput = ColorThing.yellow;
67
+ return ColorThing.yellow;
38
68
 
39
- } else if (in_range(detectedColor, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
69
+ } else if (in_range(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
40
70
 
41
71
  // red actions
42
72
 
43
- colorOutput = ColorThing.red;
73
+ return ColorThing.red;
44
74
 
45
- } else if (in_range(detectedColor, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
75
+ } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
46
76
 
47
77
  // green actions
48
78
 
49
- colorOutput = ColorThing.green;
79
+ return ColorThing.green;
50
80
 
51
- } else if (in_range(detectedColor, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
81
+ } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
52
82
 
53
83
  // blue actions
54
84
 
55
- colorOutput = ColorThing.blue;
85
+ return ColorThing.blue;
56
86
 
57
87
  } else {
58
88
 
59
89
  // actions when unreadable
60
90
 
61
- colorOutput = ColorThing.cantRead;
91
+ return ColorThing.cantRead;
62
92
 
63
93
  }
94
+
95
+ }
96
+
97
+ };
98
+
99
+
100
+
101
+ public class Main {
102
+
103
+
104
+
105
+ public static void main(String[] args) {
106
+
107
+ Color detectColor = new Color(0.2, 0.5, 0.7);
108
+
109
+ ColorThing colorThing = ColorThing.from(detectColor);
110
+
111
+ System.out.println(colorThing);
112
+
113
+ }
114
+
115
+ }
64
116
 
65
117
  ```
66
118