質問編集履歴
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,84 +1,4 @@
|
|
1
|
-
java初心者です。
|
2
|
-
|
3
|
-
0から99までの乱数を100回出して、0から9、10から19、…40から49の各範囲の数の発生回数をそれぞれ表示するプログラムを作成しました。以下がそのプログラムです。
|
4
|
-
|
5
|
-
しかし、これではながすぎるのでループを使用しようと思ったのですが、うまくいきません。
|
6
|
-
|
7
|
-
どなたかご教授ください。よろしくお願いします。
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
import java.util.Scanner;
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
public class sample {
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
public static void main(String[] args) {
|
20
|
-
|
21
|
-
Scanner stdin= new Scanner(System.in);
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
int box[] = new int[5];
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
int num = 0;
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
for(int i = 0; i < 100; i++) {
|
34
|
-
|
35
|
-
num = (int)(Math.random()*100);
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
if(num >= 0 && num <= 10){
|
40
|
-
|
41
|
-
box[0]++;
|
42
|
-
|
43
|
-
}else if
|
44
|
-
|
45
|
-
(num >= 10 && num <= 19) {
|
46
|
-
|
47
|
-
box[1]++;
|
48
|
-
|
49
|
-
}else if
|
50
|
-
|
51
|
-
(num >= 20 && num <= 29) {
|
52
|
-
|
53
|
-
box[2]++;
|
54
|
-
|
55
|
-
}else if
|
56
|
-
|
57
|
-
(num >= 30 && num <= 39) {
|
58
|
-
|
59
|
-
box[3]++;
|
60
|
-
|
61
|
-
}else if
|
62
|
-
|
63
|
-
(num >= 40 && num <= 49) {
|
64
|
-
|
65
|
-
box[4]++;
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
1
|
+
}
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
for(int k = 0; k <= 40; k += 10){
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
System.out.println( k + "→" + (k+9) +":"+ box[0]);
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
}
|
82
2
|
|
83
3
|
}
|
84
4
|
|