猿问

怎么打印出这种效果?

定义一个char类型的二维数组,并遍历这个数组,使之能打印出如下效果
*
***
*****
*******
*********
***********
*********
******
****
***
*

boy
浏览 1017回答 2
2回答

酒木君

@Test public void test0() {     //                1       3              5                   7                         9                                  11     char[][] arr = {{42}, {42,42,42}, {42,42,42,42,42}, {42,42,42,42,42,42,42}, {42,42,42,42,42,42,42,42,42}, {42,42,42,42,42,42,42,42,42,42,42}};     //                0        1             2                   3                         4                                   5          int sum = 2;     for(int i = 0; i<arr.length*2; i++) {         if(i < arr.length) {             for(int j = 0; j<arr[i].length; j++) {                 System.out.print(arr[i][j]);             }         }else if(i >= arr.length && i< (arr.length*2)-1) {             for(int j = 0; j<arr[i-sum].length; j++) {                 System.out.print(arr[i-sum][j]);             }             sum = sum + 2;         }            System.out.println();               } }

半枯

用for和if判断一下,会更好
随时随地看视频慕课网APP

相关分类

Java
我要回答