Java 用二维数组输出数字金字塔
哪位大侠能帮帮我,用二维数组存储下面的数字金字塔,然后输出:1121123211234321...
哪位大侠能帮帮我,用二维数组存储下面的数字金字塔,然后输出:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1 展开
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1 展开
1个回答
展开全部
public class Pyramid {
private static int Number = 5;
public static void main(String[] args) {
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();
}
}
}
private static int Number = 5;
public static void main(String[] args) {
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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询