二维数组存储下面的数字金字塔
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1- public class Pyramid {
- public static void main(String[] args) {
-
- int Number = 6;
- char[][] chars = new char[Number][Number * 2 - 1];
- for (int i = 0; i < Number; i++) {
- for (int j = 0; j < Number; j++) {
- if (i + j > Number - 2) {
- chars[i][j] = (char) (i + j + 2 - Number +48);
- } else
- chars[i][j] = ' ';
- }
- }
- for(int i=0;i<Number;i++){
- for(int j=Number;j<2*Number-1;j++){
- chars[i][j]=chars[i][2*Number-2-j];
- }
- }
- for(int i=0;i<Number;i++){
- for(int j=0;j<2*Number-1;j++){
- System.out.print(chars[i][j]+"\t");
- }
- System.out.println();
- }
- }
- }
复制代码
|
|