本帖最后由 凉宫蛋蛋 于 2012-7-22 15:14 编辑
- package Heima001_DumpAndDown200;
- public class Test3 {
- public static void main(String[] args) {
- //自定义行列
- int row = 7;
- int colunm = 13;
- int [][] a = new int [row][colunm];
- for(int i = 0, j = 0; i < row;i++){
-
-
- int head = 0;
- //元素行与列位置相同,遍历列,遍历长度小于最右边元素长度
- for(j = i; j < colunm - i;j++){
-
- //当元素下标等于3,到达空白处;下标等于最右边元素位置-3,可以到达空白处
- if(head == 0 || head == (colunm - 1) -2*i){
- a[i][j] = 1;
- }else{
- a[i][j] = 0;
- }
- //前面下标++,换行时,重置为0
- head++;
- }
- }
- //倒置输出上半部
- for(int i = row - 1; i > 0; i--){
- for(int j = 0; j < colunm; j++){
- if(a[i][j] == 1){
- System.out.print("*");
- }else if(a[i][j] == 0 ){
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- //输出下半部
- for(int i = 0; i < row; i++){
- for(int j = 0; j < colunm; j++){
- if(a[i][j] == 1){
- System.out.print("*");
- }else if(a[i][j] == 0 ){
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- }
- }
复制代码
这个还是比较简单的,稍微难一点的可以参考我这里发过的帖子
http://bbs.itheima.com/forum.php?mod=viewthread&tid=20122
|