多次元配列を使って遊ぶプログラム
以下のような模様を出力してほしいらしい。
■□■□■□■□■□■ □□■□■□■□■□■ ■■■□■□■□■□■ □□□□■□■□■□■ ■■■■■□■□■□■ □□□□□□■□■□■ ■■■■■■■□■□■ □□□□□□□□■□■ ■■■■■■■■■□■ □□□□□□□□□□■ ■■■■■■■■■■■
コード
import java.util.*; public class Main { public static void main(String[] args) throws Exception { final int size = 11; boolean[][] drawFlags = new boolean[size][size]; for(int line = 0; line < size; ++line){ if(line%2==0){ for(int anotherLine = 0; anotherLine <= line; ++anotherLine){ drawFlags[line][anotherLine] = true; drawFlags[anotherLine][line] = true; } } } for(int row = 0; row < size; ++row){ for(int colomn = 0; colomn < size; ++colomn){ String mark = drawFlags[colomn][row] ? "■" : "□"; System.out.print(mark); } System.out.println(); } } }
出力
■□■□■□■□■□■ □□■□■□■□■□■ ■■■□■□■□■□■ □□□□■□■□■□■ ■■■■■□■□■□■ □□□□□□■□■□■ ■■■■■■■□■□■ □□□□□□□□■□■ ■■■■■■■■■□■ □□□□□□□□□□■ ■■■■■■■■■■■
sizeを変えると描画数も変わるように作った。