ルービックキューブを印刷します
自然数n(2≤n≤9)を入力し、次のキューブマトリックスを出力する必要があります。つまり、副長さはn*n、要素は1からn*n、1は左上隅にあり、各要素は時計回りに配置されます。 n = 3の場合:
1 2 3 8 9 4 7 6 5
[入力フォーム]標準入力から整数nを読み取ります。
[出力フォーム]結果を標準の出力に印刷します。要件を満たす正方行列は出力、各数値は幅5文字を占有し、右に整列し、各ラインの最後にキャリッジリターン文字が出力されます。
【入力サンプル】4
【出力サンプル】
1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
成し遂げる:
パッケージcn.dfeng; java.util.arraysをインポートします。 Java.util.scannerをインポートします。パブリッククラスの迷路{enum方向{up、down、右、左; } public int [] [] buidmaze(int n){int [] [] maze = new int [n] [n]; for(int [] a:maze){arrays.fill(a、0); } int col = 0; int row = 0; int counter = 1;方向d =方向.right; while(true){if(maze [row] [col] == 0){Maze [row] [col] = counter ++; switch(d){case right:if(col + 1 <n && maze [row] [col + 1] == 0){col ++; } else {d = direction.down;行++; } 壊す;ケースダウン:if(row + 1 <n && maze [row + 1] [col] == 0){row ++; } else {d = direction.left; col-; } 壊す;ケース左:if(col -1> = 0 && maze [row] [col -1] == 0){col-; } else {d = direction.up;行 - ; } 壊す;デフォルト:if(row -1> = 0 && maze [row -1] [col] == 0){row-; } else {d = direction.right; col ++; } 壊す; }} else {break; }}迷路を返します。 } public void printmaze(int [] [] aze){for(int [] row:maze){for(int i:row){system.out.printf( "%3d"、i); } system.out.println(); }} / ** * @param args * / public static void main(string [] args){scanner sc = new scanner(system.in); System.out.println( "迷路のサイズを入力してください:"); int size = sc.nextint();迷路迷路= new Maze(); int [] [] m = maze.buidmaze(size); Maze.Printmaze(M); }}ダイヤモンドグラフィックを印刷します
ダイヤモンドの写真の効果はおそらく次のようなものです。
以下のコードを見てみましょう
パッケージcn.dfeng; / ***このクラスは*を使用して** @Author dfeng**/ public classドロワーでダイヤモンドグラフィックを印刷できます。 int i = 0;ブールフラグ= true; while(i> = 0){if(i <n){for(int j = 0; j <n -i; j ++){system.out.print( ""); } for(int j = n -i; j <= n + i; j + = 2){system.out.print( "*"); } system.out.println(); } if(i == n){flag = false;私 - ; } if(flag){i ++; } else {i--; }}}}