teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

日本語の修正

2021/08/14 07:18

投稿

koklopkiujio
koklopkiujio

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  ### 前提・実現したいこと
2
- 2つのfor文を1つのfor文に置き換えたいです。
2
+ 関数next内前半のfor文と後半の入れ子になっているfor文つの入れ子になっているfor文にまとめたいです。
3
3
  なんかかっこ悪いのでスマートな書き方があったら教えてください。
4
4
  hyoはvかけるvの二次元配列
5
5
  now_gyo,now_retuはグローバル変数

2

コード全体を乗せた

2021/08/14 07:18

投稿

koklopkiujio
koklopkiujio

スコア2

title CHANGED
File without changes
body CHANGED
@@ -16,19 +16,41 @@
16
16
 
17
17
  ```java
18
18
 
19
+     import java.util.Arrays;
20
+
21
+ public class hogehoge {
22
+ public static int now_gyo=1;
23
+ public static int now_retu=2;
24
+ public static int v=5;
25
+ public static void main(String[] args) {
26
+ next();
27
+
28
+ }
29
+ public static void next() {
30
+
31
+ int[][] hyo=new int[][]{
32
+ {0 ,-1, -1 , 5 ,-1},
33
+ {-1 , 0, -1 , 4 , 5},
34
+ {-1 ,-1, 0 , 2 ,-1},
35
+ {-1 ,-1, -1 , 0 ,-1},
36
+ {-1 ,-1, -1 ,-1 , 0}
37
+ };
19
-     for (int i = now_retu+1; i < v; i++) {
38
+ for (int i = now_retu+1; i < v; i++) {
20
- if(hyo[now_gyo][i]==-1){
39
+ if(hyo[now_gyo][i]==-1){
21
- now_retu=i;
40
+ now_retu=i;
22
- return;
41
+ return;
23
- }
42
+ }
24
- }
43
+ }
25
- for (int i = now_gyo+1; i < v; i++) {
44
+ for (int i = now_gyo+1; i < v; i++) {
26
- for (int j = i+1; j < v; j++) {
45
+ for (int j = i+1; j < v; j++) {
27
- if(hyo[i][j]==-1){
46
+ if(hyo[i][j]==-1){
28
- now_gyo=i;
47
+ now_gyo=i;
29
- now_retu=j;
48
+ now_retu=j;
30
- return;
49
+ return;
31
- }
50
+ }
32
- }
51
+ }
33
- }
52
+ }
53
+ }
54
+ }
55
+
34
56
  ```

1

xをnow_gyo,yを now_retuに置換しました

2021/08/14 07:03

投稿

koklopkiujio
koklopkiujio

スコア2

title CHANGED
File without changes
body CHANGED
@@ -16,17 +16,17 @@
16
16
 
17
17
  ```java
18
18
 
19
-     for (int i = y+1; i < v; i++) {
19
+     for (int i = now_retu+1; i < v; i++) {
20
- if(hyo[x][i]==-1){
20
+ if(hyo[now_gyo][i]==-1){
21
- x=i;
21
+ now_retu=i;
22
22
  return;
23
23
  }
24
24
  }
25
- for (int i = x+1; i < v; i++) {
25
+ for (int i = now_gyo+1; i < v; i++) {
26
26
  for (int j = i+1; j < v; j++) {
27
27
  if(hyo[i][j]==-1){
28
- x=i;
28
+ now_gyo=i;
29
- y=j;
29
+ now_retu=j;
30
30
  return;
31
31
  }
32
32
  }