LoginSignup
0
2

More than 1 year has passed since last update.

【Java】ビンゴカード作成問題を解く

Last updated at Posted at 2021-08-11

 僕は現在Javaを勉強中です。ということで過去にRubyで解いた問題をJavaで解いてみることにしました。RubyもJavaもまだ初心者であるため、コードの可読性は高くなく、まずは動くだけのプログラムを作ることに努めています。ご意見いただけると非常に勉強になります。

 今回解いた問題はビンゴカード作成問題です。

B:1~15のどれか
I:16~30のどれか
N:31~45のどれか
G:46~60のどれか
O:61~75のどれか

 B |  I |  N |  G |  O
13 | 22 | 32 | 48 | 61
 3 | 23 | 43 | 53 | 63
 4 | 19 |    | 60 | 65
12 | 16 | 44 | 50 | 75
 2 | 28 | 33 | 56 | 68

 このような出力結果を得たいという問題になります。僕は以下のようなコードでこの問題を解きました。

package example;

import java.util.ArrayList;
import java.util.Collections;

public class Bingo {

    public static void main(String[] args) {

        ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();
        int num = 1;
        for (int count = 1; count <= 5; count++) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = num; i <= num + 14; i++) {
                list.add(i);
            }
            Collections.shuffle(list);
            lists.add(list);
            num += 15;
        }

        System.out.println("  B |  I |  N |  G |  O");

        for (int count = 0; count <= 4; count++) {
            for (int i = 0; i <= 4; i++) {
                if (count == 2 && i == 2) {
                    System.out.print("    |");
                } else {
                    System.out.printf("%3d", lists.get(i).get(count));
                    if (i != 4) {
                        System.out.print(" |");
                    }
                }
            }
            System.out.println("\n");

        }

    }

}

 理屈としては以下のコードの重複した部分をまとめた形となります。

package example;

import java.util.ArrayList;
import java.util.Collections;

public class Bingo {

    public static void main(String[] args) {
        System.out.println("  B |  I |  N |  G |  O");
        ArrayList<Integer> list15 = new ArrayList<Integer>();
        ArrayList<Integer> list30 = new ArrayList<Integer>();
        ArrayList<Integer> list45 = new ArrayList<Integer>();
        ArrayList<Integer> list60 = new ArrayList<Integer>();
        ArrayList<Integer> list75 = new ArrayList<Integer>();

        for(int count = 1 ; count <= 15 ; count++) {
            list15.add(count);
        }

        for(int count = 16 ; count <= 30 ; count++) {
            list30.add(count);
        }

        for(int count = 31 ; count <= 45 ; count++) {
            list45.add(count);
        }

        for(int count = 46 ; count <= 60 ; count++) {
            list60.add(count);
        }

        for(int count = 61 ; count <= 75 ; count++) {
            list75.add(count);
        }

        Collections.shuffle(list15);
        Collections.shuffle(list30);
        Collections.shuffle(list45);
        Collections.shuffle(list60);
        Collections.shuffle(list75);

        for (int count = 0; count <= 4; count++) {
            System.out.printf("%3d", list15.get(count));
            System.out.print(" |");
            System.out.printf("%3d", list30.get(count));
            System.out.print(" |");

            if (count == 2) {
                System.out.print("    |");
            } else {
                System.out.printf("%3d", list45.get(count));
                System.out.print(" |");
            }

            System.out.printf("%3d", list60.get(count));
            System.out.print(" |");
            System.out.printf("%3d", list75.get(count));
            System.out.println("\n");
        }

    }

}

 

 ※コメントを受けて修正いたしました!

package example;

import java.util.ArrayList;
import java.util.Collections;

public class Bingo {

    public static void main(String[] args) {

        ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();
        int num = 1;
        for (int i = 0; i <= 4; i++) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int j = num; j <= num + 14; j++) {
                list.add(j);
            }
            Collections.shuffle(list);
            lists.add(list);
            num += 15;
        }

        System.out.println("  B |  I |  N |  G |  O");

        for (int i = 0; i <= 4; i++) {
            for (int j = 0; j <= 4; j++) {
                if (i == 2 && j == 2) {
                    System.out.print("   ");
                } else {
                    System.out.printf("%3d", lists.get(j).get(i));
                }
                if (j != 4) {
                    System.out.print(" |");
                }
            }
            System.out.println();

        }

    }

}

 まだ至らぬ点はあるかと思いますので、コメントいただけるとうれしいです。

0
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2