回答編集履歴
2
説明の訂正
answer
CHANGED
@@ -3,75 +3,53 @@
|
|
3
3
|
1.最終的に表示したい数字を格納する2次配列arrayとcountという配列を準備します。
|
4
4
|
このcountという配列は1~9の数字が最初は1個ずつあり、使うと0になる
|
5
5
|
それを記憶する配列です。つまり初期化ですべて1にします。
|
6
|
+
これをメソッドInit()に記述します。
|
6
7
|
|
7
|
-
```Java
|
8
|
-
int [][]array = new int[3][3];
|
9
|
-
|
10
|
-
int []count = new int[9];
|
11
|
-
|
12
|
-
for(int i = 0; i < count.length; i++){
|
13
|
-
count[i] = 1;
|
14
|
-
}
|
15
|
-
```
|
16
8
|
2.ここからが重要なところです。
|
17
9
|
2重for文でarrayにそれぞれ乱数を格納するとき重複を避けるため
|
18
10
|
do~while文を使います。
|
19
11
|
(i)count配列のi番目が0でなければその乱数が格納され 使われた乱数のcountは0になります。
|
20
12
|
(ii)count配列のi番目が0ならばすでに使われたということなのでdo~while文でまだ使われていない
|
21
13
|
乱数が出るまでループします。
|
14
|
+
(iii)一つの列の格納が終わったら、メソッドInit()で初期化します。
|
22
15
|
|
23
|
-
```Java
|
24
|
-
Random rand = new Random();
|
25
|
-
for(int i = 0; i < 3; i++){
|
26
|
-
for(int j = 0; j < 3; j++){
|
27
|
-
do{
|
28
|
-
array[i][j] = rand.nextInt(9) + 1;
|
29
|
-
|
16
|
+
3.あとは1~9の乱数が格納されたarrayを2重for文で表示すれば完了です。
|
30
17
|
|
31
|
-
count[array[i][j] - 1]--;
|
32
|
-
}
|
33
|
-
}
|
34
|
-
```
|
35
|
-
|
36
|
-
4.あとは1~9の乱数が格納されたarrayを2重for文で表示すれば完了です。
|
37
|
-
|
38
|
-
|
39
18
|
```Java
|
40
19
|
import java.util.Random;
|
41
20
|
|
42
21
|
public class Main{
|
43
|
-
public static void main(String[] args)
|
22
|
+
public static void main(String[] args){
|
44
|
-
|
23
|
+
Random rand = new Random();
|
45
|
-
|
24
|
+
int [][]array = new int[3][3];
|
46
|
-
|
47
|
-
|
25
|
+
int []count = new int[9];
|
48
|
-
|
26
|
+
Init(count);
|
27
|
+
|
28
|
+
for(int i = 0; i < 3; i++){
|
29
|
+
Init(count);
|
30
|
+
for(int j = 0; j < 3; j++){
|
31
|
+
do{
|
32
|
+
array[i][j] = rand.nextInt(9) + 1;
|
33
|
+
}while(count[array[i][j] - 1] == 0);
|
34
|
+
count[array[i][j] - 1]--;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
for(int i = 0; i < 3; i++){
|
38
|
+
for(int j = 0; j < 3; j++){
|
39
|
+
System.out.print(array[i][j] + " ");
|
40
|
+
}
|
41
|
+
System.out.println();
|
42
|
+
}
|
43
|
+
}
|
44
|
+
public static void Init(int[] count){
|
49
|
-
|
45
|
+
for(int i = 0; i < count.length; i++){
|
50
|
-
|
46
|
+
count[i] = 1;
|
51
|
-
|
47
|
+
}
|
52
|
-
|
53
|
-
Random rand = new Random();
|
54
|
-
|
55
|
-
for(int i = 0; i < 3; i++){
|
56
|
-
for(int j = 0; j < 3; j++){
|
57
|
-
do{
|
58
|
-
array[i][j] = rand.nextInt(9) + 1;
|
59
|
-
}while(count[array[i][j] - 1] == 0);
|
60
|
-
count[array[i][j] - 1]--;
|
61
|
-
}
|
62
|
-
}
|
63
|
-
for(int i = 0; i < 3; i++){
|
64
|
-
for(int j = 0; j < 3; j++){
|
65
|
-
System.out.print(array[i][j] + " ");
|
66
|
-
}
|
67
|
-
System.out.println();
|
68
|
-
}
|
69
|
-
|
70
48
|
}
|
71
49
|
}
|
72
50
|
```
|
73
51
|
|
74
52
|
<表示結果の例>
|
75
|
-
1 3 8
|
76
|
-
9
|
53
|
+
9 5 2
|
77
|
-
|
54
|
+
5 1 3
|
55
|
+
7 8 1
|
1
説明の訂正
answer
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
こんにちは、
|
2
2
|
|
3
3
|
1.最終的に表示したい数字を格納する2次配列arrayとcountという配列を準備します。
|
4
|
-
このcountという配列は
|
4
|
+
このcountという配列は1~9の数字が最初は1個ずつあり、使うと0になる
|
5
5
|
それを記憶する配列です。つまり初期化ですべて1にします。
|
6
6
|
|
7
7
|
```Java
|
@@ -33,7 +33,7 @@
|
|
33
33
|
}
|
34
34
|
```
|
35
35
|
|
36
|
-
4.あとは
|
36
|
+
4.あとは1~9の乱数が格納されたarrayを2重for文で表示すれば完了です。
|
37
37
|
|
38
38
|
|
39
39
|
```Java
|