数组实现矩阵逐层向内层加1
package java1701;
public class javaMain {
?? ?public static void main(String[] args) {
?? ??? ?// 逐層加
?? ??? ?// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
?? ??? ?// 1 2 2 2 2 1 1 2 2 2 1 1 2 2 1 1 2 1
?? ??? ?// 1 2 3 3 2 1 1 2 3 2 1 1 2 2 1 1 1 1
?? ??? ?// 1 2 3 3 2 1 1 2 2 2 1 1 1 1 1
?? ??? ?// 1 2 2 2 2 1 1 1 1 1 1
?? ??? ?// 1 1 1 1 1 1
?? ??? ?show();
?? ?}
?? ?public static void show() {
?? ??? ?int w = 11;//圖形寬度
?? ??? ?int[][] arr = new int[w][w];
?? ?
?? ??? ?for (int ceng = 1; ceng <= (w + 1) / 2; ceng++) {
?? ??? ??? ?doWork(ceng, w ,arr);//i是數(shù)字層數(shù)及數(shù)字
?? ??? ?}
?? ??? ?//打印數(shù)組
?? ??? ?for (int i = 0; i < arr.length; i++) {
?? ??? ??? ?for (int j = 0; j < arr[i].length; j++) {
?? ??? ??? ??? ?System.out.print(arr[i][j]+" ");
?? ??? ??? ?}
?? ??? ??? ?System.out.println();
?? ??? ?}
?? ?}
?? ?public static void doWork(int ceng, int width,int[][] arr) {
?? ??? ?for (int i = ceng-1; i < width-ceng+1; i++) {
?? ??? ??? ?for (int j = ceng-1; j < width-ceng+1; j++) {
?? ??? ??? ??? ?arr[i][j]=ceng;
?? ??? ??? ?}
?? ??? ?}?? ??? ?
?? ?}
}
轉(zhuǎn)載于:https://blog.51cto.com/10927283/1900559
總結(jié)
以上是生活随笔為你收集整理的数组实现矩阵逐层向内层加1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Windows常用运行库--VC++、D
- 下一篇: Makefile函数使用