回答編集履歴

2

説明の訂正

2018/04/24 10:17

投稿

退会済みユーザー
test CHANGED
@@ -8,25 +8,9 @@
8
8
 
9
9
  それを記憶する配列です。つまり初期化ですべて1にします。
10
10
 
11
+ これをメソッドInit()に記述します。
11
12
 
12
13
 
13
- ```Java
14
-
15
- int [][]array = new int[3][3];
16
-
17
-
18
-
19
- int []count = new int[9];
20
-
21
-
22
-
23
- for(int i = 0; i < count.length; i++){
24
-
25
- count[i] = 1;
26
-
27
- }
28
-
29
- ```
30
14
 
31
15
  2.ここからが重要なところです。
32
16
 
@@ -40,37 +24,11 @@
40
24
 
41
25
  乱数が出るまでループします。
42
26
 
43
-
44
-
45
- ```Java
46
-
47
- Random rand = new Random();
48
-
49
- for(int i = 0; i < 3; i++){
50
-
51
- for(int j = 0; j < 3; j++){
52
-
53
- do{
54
-
55
- array[i][j] = rand.nextInt(9) + 1;
27
+ (iii)一つの列の格納が終わったら、メソッドInit()で初期化します。
56
-
57
- }while(count[array[i][j] - 1] == 0);
58
28
 
59
29
 
60
30
 
61
- count[array[i][j] - 1]--;
62
-
63
- }
64
-
65
- }
66
-
67
- ```
68
-
69
-
70
-
71
- 4.あとは1~9の乱数が格納されたarrayを2重for文で表示すれば完了です。
31
+ 3.あとは1~9の乱数が格納されたarrayを2重for文で表示すれば完了です。
72
-
73
-
74
32
 
75
33
 
76
34
 
@@ -82,59 +40,57 @@
82
40
 
83
41
  public class Main{
84
42
 
85
- public static void main(String[] args) {
43
+ public static void main(String[] args){
86
44
 
87
-
45
+ Random rand = new Random();
88
46
 
89
- int [][]array = new int[3][3];
47
+ int [][]array = new int[3][3];
90
48
 
91
-
49
+ int []count = new int[9];
92
50
 
93
- int []count = new int[9];
51
+ Init(count);
94
52
 
95
-
53
+
96
54
 
97
- for(int i = 0; i < count.length; i++){
55
+ for(int i = 0; i < 3; i++){
98
56
 
99
- count[i] = 1;
57
+ Init(count);
100
58
 
101
- }
59
+ for(int j = 0; j < 3; j++){
102
60
 
103
-
61
+ do{
104
62
 
105
- Random rand = new Random();
63
+ array[i][j] = rand.nextInt(9) + 1;
106
64
 
65
+ }while(count[array[i][j] - 1] == 0);
107
66
 
67
+ count[array[i][j] - 1]--;
108
68
 
109
- for(int i = 0; i < 3; i++){
69
+ }
110
70
 
111
- for(int j = 0; j < 3; j++){
71
+ }
112
72
 
113
- do{
73
+ for(int i = 0; i < 3; i++){
114
74
 
115
- array[i][j] = rand.nextInt(9) + 1;
75
+ for(int j = 0; j < 3; j++){
116
76
 
117
- }while(count[array[i][j] - 1] == 0);
77
+ System.out.print(array[i][j] + " ");
118
78
 
119
- count[array[i][j] - 1]--;
79
+ }
120
80
 
121
- }
81
+ System.out.println();
122
82
 
123
- }
83
+ }
124
84
 
125
- for(int i = 0; i < 3; i++){
85
+ }
126
86
 
127
- for(int j = 0; j < 3; j++){
87
+ public static void Init(int[] count){
128
88
 
129
- System.out.print(array[i][j] + " ");
89
+ for(int i = 0; i < count.length; i++){
130
90
 
131
- }
91
+ count[i] = 1;
132
92
 
133
- System.out.println();
134
-
135
- }
93
+ }
136
-
137
-
138
94
 
139
95
  }
140
96
 
@@ -146,8 +102,8 @@
146
102
 
147
103
  <表示結果の例>
148
104
 
149
- 1 3 8
105
+ 9 5 2
150
106
 
151
- 9 4 5
107
+ 5 1 3
152
108
 
153
- 6 7 2
109
+ 7 8 1

1

説明の訂正

2018/04/24 10:17

投稿

退会済みユーザー
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  1.最終的に表示したい数字を格納する2次配列arrayとcountという配列を準備します。
6
6
 
7
- このcountという配列は0~9の数字が最初は1個ずつあり、使うと0になる
7
+ このcountという配列は1~9の数字が最初は1個ずつあり、使うと0になる
8
8
 
9
9
  それを記憶する配列です。つまり初期化ですべて1にします。
10
10
 
@@ -68,7 +68,7 @@
68
68
 
69
69
 
70
70
 
71
- 4.あとは0~9の乱数が格納されたarrayを2重for文で表示すれば完了です。
71
+ 4.あとは1~9の乱数が格納されたarrayを2重for文で表示すれば完了です。
72
72
 
73
73
 
74
74