回答編集履歴

3

補足

2016/07/05 23:15

投稿

matobaa
matobaa

スコア2493

test CHANGED
@@ -1,3 +1,7 @@
1
+ 厨二的に。
2
+
3
+
4
+
1
5
  とりあえず前半だけ
2
6
 
3
7
 

2

SimpleEntryで作ってみた

2016/07/05 23:15

投稿

matobaa
matobaa

スコア2493

test CHANGED
@@ -116,4 +116,70 @@
116
116
 
117
117
  やっぱりきれいじゃない。クラス作らなきゃいけないもんかなあ。
118
118
 
119
+ SimpleEntry 使ってみる:
120
+
121
+
122
+
123
+ ```java
124
+
125
+ package teratail;
126
+
127
+
128
+
129
+ import java.util.AbstractMap.SimpleEntry;
130
+
131
+ import java.util.Arrays;
132
+
133
+ import java.util.Comparator;
134
+
135
+
136
+
137
+ public class Teratail40137 {
138
+
139
+
140
+
141
+ static SimpleEntry<SimpleEntry, Double> reportAndRatio(SimpleEntry e) {
142
+
143
+ int[] v = (int[]) e.getValue();
144
+
145
+ double r = (double) v[1] / (double) v[0];
146
+
147
+ System.out.printf("%sは、今季 6 月 15 日現在 %s打数 %s安打で,打率 %fです.\n", e.getKey(), v[0], v[1], r);
148
+
149
+ return new SimpleEntry(e, r);
150
+
151
+ }
152
+
153
+
154
+
155
+ public static void main(String[] args) {
156
+
157
+ SimpleEntry[] players = {
158
+
159
+ new SimpleEntry("鳥谷", new int[]{239, 80}),
160
+
161
+ new SimpleEntry("大島", new int[]{267, 90}),
162
+
163
+ new SimpleEntry("糸井", new int[]{231, 82}),
164
+
165
+ new SimpleEntry("柳田", new int[]{217, 73}) };
166
+
167
+
168
+
169
+ Arrays.stream(players)
170
+
171
+ .map(Teratail40137::reportAndRatio)
172
+
173
+ .max(Comparator.comparing(x -> x.getValue()))
174
+
175
+ .ifPresent(x -> {System.out.println("打率が最も高いのは: " + x.getKey().getKey());});
176
+
177
+ }
178
+
179
+ }
180
+
181
+ ```
182
+
183
+
184
+
119
- いろいろ勉強になった。
185
+ こんなもんかな。いろいろ勉強になった。

1

後半も作った

2016/07/05 23:13

投稿

matobaa
matobaa

スコア2493

test CHANGED
@@ -31,3 +31,89 @@
31
31
 
32
32
 
33
33
  んー。なんかきれいじゃないな
34
+
35
+
36
+
37
+ 後半も作ってみた:
38
+
39
+
40
+
41
+ ```java
42
+
43
+ package teratail;
44
+
45
+
46
+
47
+ import java.util.Arrays;
48
+
49
+ import java.util.Comparator;
50
+
51
+ import java.util.Formatter;
52
+
53
+ import java.util.stream.Collectors;
54
+
55
+
56
+
57
+ public class Teratail40137 {
58
+
59
+ public static void main(String[] args) {
60
+
61
+ Player[] players = { new Player("鳥谷", 239, 80), new Player("大島", 267, 90), new Player("糸井", 231, 82), new Player("柳田", 217, 73) };
62
+
63
+ System.out.println(Arrays.stream(players).map(Player::toString).collect(Collectors.joining()));
64
+
65
+ Arrays.stream(players).max(Comparator.comparing(x -> x.ratio())).ifPresent(x -> {System.out.println("打率が最も高いのは: " + x.name);});
66
+
67
+ }
68
+
69
+ }
70
+
71
+
72
+
73
+ class Player {
74
+
75
+ final String name;
76
+
77
+ final int sheet;
78
+
79
+ final int hit;
80
+
81
+
82
+
83
+ public Player(String name, int sheet, int hit) {
84
+
85
+ this.name = name;
86
+
87
+ this.sheet = sheet;
88
+
89
+ this.hit = hit;
90
+
91
+ }
92
+
93
+
94
+
95
+ double ratio() {
96
+
97
+ return (double) hit / (double) sheet;
98
+
99
+ }
100
+
101
+
102
+
103
+ public String toString() {
104
+
105
+ try (Formatter f = new Formatter()) {
106
+
107
+ return f.format("%sは、今季 6 月 15 日現在 %s 打数 %s安打で,打率 %fです.\n", name, sheet, hit, ratio()).toString();
108
+
109
+ }
110
+
111
+ }
112
+
113
+ }
114
+
115
+ ```
116
+
117
+ やっぱりきれいじゃない。クラス作らなきゃいけないもんかなあ。
118
+
119
+ いろいろ勉強になった。