質問編集履歴

3

ソースコードの更新

2019/04/15 06:12

投稿

nanimowakaranai
nanimowakaranai

スコア20

test CHANGED
File without changes
test CHANGED
@@ -131,3 +131,117 @@
131
131
  ```
132
132
 
133
133
  よろしくお願いいたします。
134
+
135
+ 追記
136
+
137
+ ```java
138
+
139
+
140
+
141
+ public class MyWeight{
142
+
143
+ private int ton;
144
+
145
+ private int kgram;
146
+
147
+ private int gram;
148
+
149
+
150
+
151
+
152
+
153
+ public void setWeight(int t,int kg,int gr) {
154
+
155
+ ton = t + ((kg + (gr/1000)) /1000) ;
156
+
157
+ kgram = (kg + gr/1000) % 1000;
158
+
159
+ gram = gr % 1000;
160
+
161
+
162
+
163
+ }
164
+
165
+ public int getT() {
166
+
167
+ return ton;
168
+
169
+ }
170
+
171
+ public int getKG() {
172
+
173
+ return kgram;
174
+
175
+ }
176
+
177
+ public int getG(){
178
+
179
+ return gram;
180
+
181
+ }
182
+
183
+ public MyWeight add(MyWeight x) {
184
+
185
+ int t = 0,kg = 0,gr = 0;
186
+
187
+ t = t+ x.getT();
188
+
189
+ kg = kg + x.getKG();
190
+
191
+ gr = gr+ x.getG();
192
+
193
+ MyWeight y = new MyWeight();
194
+
195
+ y.setWeight( t, kg, gr);
196
+
197
+ return y;
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+
206
+
207
+ public static void main(String args[]) {
208
+
209
+ int tonn[] = { 20, 30, 10, 20, 20 };
210
+
211
+ int kgr[] = { 250, 780, 190, 360, 450 };
212
+
213
+ int gramu[] = { 310, 400, 960, 210, 730 };
214
+
215
+
216
+
217
+ MyWeight w=new MyWeight();
218
+
219
+ MyWeight sum = new MyWeight();
220
+
221
+
222
+
223
+ for(int i=0;i<5;i++) {
224
+
225
+ w.setWeight(tonn[i],kgr[i],gramu[i]);
226
+
227
+ sum.setWeight(tonn[i],kgr[i],gramu[i]);
228
+
229
+ sum = sum.add(w);
230
+
231
+
232
+
233
+ }
234
+
235
+ System.out.println("sum"+sum.getT()+"t"+sum.getKG()+"kg"+sum.getG()+"g");
236
+
237
+ }
238
+
239
+ }
240
+
241
+ ```
242
+
243
+ *すみません、下の方のクラスが見つからなかった問題は解決しました。
244
+
245
+
246
+
247
+ 回答を参考に以上のように修正してみたのですが、[4]番目の配列しか表示されませんでした.すべてのすべての配列を合計したのを表示したいのですがよくわかりません。

2

最期のSystem文の変更

2019/04/15 06:12

投稿

nanimowakaranai
nanimowakaranai

スコア20

test CHANGED
File without changes
test CHANGED
@@ -120,7 +120,7 @@
120
120
 
121
121
  }
122
122
 
123
- System.out.println("合計は"+sum+"トンです");
123
+ System.out.println("合計は"+sum.getT()+"トン"+sum.getKG()+"キログラム"+sum.getG()+"です");
124
124
 
125
125
  }
126
126
 

1

ソースコードの訂正

2019/04/15 05:29

投稿

nanimowakaranai
nanimowakaranai

スコア20

test CHANGED
File without changes
test CHANGED
@@ -30,103 +30,103 @@
30
30
 
31
31
  ```java
32
32
 
33
+ public class Test{
33
34
 
35
+ class MyWeight{
34
36
 
35
- class MyWeight{
37
+ private int ton;
36
38
 
37
- private int ton;
39
+ private int kgram;
38
40
 
39
- private int kgram;
40
-
41
- private int gram;
41
+ private int gram;
42
42
 
43
43
 
44
44
 
45
45
 
46
46
 
47
- public void setWeight(int t,int kg,int gr) {
47
+ public void setWeight(int t,int kg,int gr) {
48
48
 
49
- ton = t + ((kg + (gr/1000)) /1000) ;
49
+ ton = t + ((kg + (gr/1000)) /1000) ;
50
50
 
51
- kgram = (kg + gr/1000) % 1000;
51
+ kgram = (kg + gr/1000) % 1000;
52
52
 
53
- gram = gr % 1000;
53
+ gram = gr % 1000;
54
54
 
55
55
 
56
56
 
57
- }
57
+ }
58
58
 
59
- public int getT() {
59
+ public int getT() {
60
60
 
61
- return ton;
61
+ return ton;
62
+
63
+ }
64
+
65
+ public int getKG() {
66
+
67
+ return kgram;
68
+
69
+ }
70
+
71
+ public int getG(){
72
+
73
+ return gram;
74
+
75
+ }
76
+
77
+ public MyWeight add(MyWeight x) {
78
+
79
+ int t = 0,kg = 0,gr = 0;
80
+
81
+ t = t+ x.getT();
82
+
83
+ kg = kg + x.getKG();
84
+
85
+ gr = gr+ x.getG();
86
+
87
+ MyWeight y = new MyWeight();
88
+
89
+ y.setWeight( t, kg, gr);
90
+
91
+ return y;
92
+
93
+
94
+
95
+ }
96
+
97
+
98
+
99
+ public void main(String args[]) {
100
+
101
+ int tonn[] = { 20, 30, 10, 20, 20 };
102
+
103
+ int kgr[] = { 250, 780, 190, 360, 450 };
104
+
105
+ int gramu[] = { 310, 400, 960, 210, 730 };
106
+
107
+
108
+
109
+ MyWeight w=new MyWeight();
110
+
111
+ MyWeight sum;
112
+
113
+
114
+
115
+ for(int i=0;i<4;i++) {
116
+
117
+ w.setWeight(tonn[i],kgr[i],gramu[i]);
118
+
119
+ sum = sum + w.add(w);
120
+
121
+ }
122
+
123
+ System.out.println("合計は"+sum+"トンです");
124
+
125
+ }
62
126
 
63
127
  }
64
128
 
65
- public int getKG() {
66
-
67
- return kgram;
68
-
69
- }
70
-
71
- public int getG(){
72
-
73
- return gram;
74
-
75
- }
76
-
77
- public MyWeight add(MyWeight x) {
78
-
79
- int t = 0,kg = 0,gr = 0;
80
-
81
- t = t+ x.getT();
82
-
83
- kg = kg + x.getKG();
84
-
85
- gr = gr+ x.getG();
86
-
87
- MyWeight y = new MyWeight();
88
-
89
- y.setWeight( t, kg, gr);
90
-
91
- return y;
92
-
93
-
94
-
95
- }
96
-
97
-
98
-
99
- public static void main(String args[]) {
100
-
101
- int tonn[] = { 20, 30, 10, 20, 20 };
102
-
103
- int kgr[] = { 250, 780, 190, 360, 450 };
104
-
105
- int gramu[] = { 310, 400, 960, 210, 730 };
106
-
107
-
108
-
109
- MyWeight w=new MyWeight();
110
-
111
- MyWeight sum;
112
-
113
-
114
-
115
- for(int i=0;i<4;i++) {
116
-
117
- w.setWeight(tonn[i],kgr[i],gramu[i]);
118
-
119
- sum = sum + w.add(w);
120
-
121
- }
122
-
123
- System.out.println("合計は"+sum+"トンです");
124
-
125
- }
126
-
127
129
  }
128
-
129
-
130
130
 
131
131
  ```
132
132