X印を描画する、Javaで
概要
コマンドラインで指定する大きさのX印を描画する。
public class XDrawer { static int size; static int height, width; public static void main(String[] args) { int i, j; size = Integer.parseInt(args[0]); height = size; width = height; char drawable[][]; drawable = new char[height][width]; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { drawable[i][j] = '.'; } } for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { if (checkPosition(j, i)) { drawable[j][i] = 'X'; } } } for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { System.out.print(drawable[j][i]); } System.out.println(); } } static boolean checkPosition(int j, int i) { boolean plot = false; if (i == j || i + j == height) { plot = true; } else { plot = false; } return plot; } }
実行結果(コマンドライン引数に21を入力した場合)
X....................
.X..................X
..X................X.
...X..............X..
....X............X...
.....X..........X....
......X........X.....
.......X......X......
........X....X.......
.........X..X........
..........XX.........
..........XX.........
.........X..X........
........X....X.......
.......X......X......
......X........X.....
.....X..........X....
....X............X...
...X..............X..
..X................X.
.X..................X