本帖最后由 凉宫蛋蛋 于 2012-7-23 01:09 编辑  
 
 
 
- public class Triangle {
 
 -     public static void main(String[] args) {
 
 -         // TODO Auto-generated method stub
 
 -         int row = 8;
 
 -         int colunm = 8;
 
 -         int f = 1;
 
 -         int row2 = row / 2;
 
 -         int colunm2 = colunm / 2;
 
 -         // 1****************左上三角
 
 -         System.out.println("--1左上三角--" + f++ + "---");
 
 -         for (int i = 0; i < row; i++) {
 
 -             for (int j = 0; j < colunm - i; j++) {
 
 -                 System.out.print("*");
 
 -             }
 
 -             System.out.println("");
 
 -         }
 
  
-         System.out.println("");
 
 -         System.out.println("");
 
  
-         // 2****************左下三角
 
 -         System.out.println("-2左下三角---" + f++ + "---");
 
 -         for (int i = 0; i < row; i++) {
 
 -             for (int j = 0; j < i; j++) {
 
 -                 System.out.print("*");
 
 -             }
 
 -             System.out.println("");
 
 -         }
 
  
-         System.out.println("");
 
 -         System.out.println("");
 
  
-         // 3****************左下右上组合,把-符号改了就是右上了
 
 -         System.out.println("-3左下右上组合---" + f++ + "---");
 
 -         for (int i = 0; i < row; i++) {
 
 -             // 换行的时候,每次做内层的时候j必须要清0
 
 -             int head = 0;
 
 -             // 外层循环只要循环他要循环的就可以了,用边界条件限制
 
 -             // 因为是先做内层循环的事情的
 
 -             for (int j = 0; j < colunm - i; j++) {
 
 -                 while (head < i) {
 
 -                     System.out.print("-");
 
 -                     head++;
 
 -                 }
 
 -                 System.out.print("*");
 
 -             }
 
 -             System.out.println("");
 
 -         }
 
  
-         System.out.println("");
 
 -         System.out.println("");
 
  
-         // 4****************右下左上组合,把-符号改了就是右下了
 
 -         System.out.println("-4右下左上组合---" + f++ + "---");
 
 -         for (int i = 0, j = 0; i < row; i++) {
 
 -             // 换行的时候,每次做内层的时候j必须要清0
 
 -             j = 0;
 
 -             for (int k = 0; k < colunm - j + 1; k++) {
 
 -                 while (j < colunm - i) {
 
 -                     System.out.print("-");
 
 -                     j++;
 
 -                 }
 
 -                 System.out.print("*");
 
 -             }
 
 -             System.out.println("");
 
 -         }
 
 -         System.out.println("");
 
 -         System.out.println("");
 
 -     }
 
  
- }
 
  
  复制代码- public class Test8 {
 
 -     public static void main(String[] args) {
 
 -         // 自定义行列,行数必须大于等于列数,形状才符合
 
 -         int row = 24;
 
 -         int colunm = 24;
 
 -         int row2 = row / 2;
 
 -         int colunm2 = colunm / 2;
 
  
-         System.out.println("********************************************");
 
 -         System.out.println("");
 
 -         //***********上半部分左边右边大结合
 
 -         for (int i = 0, jL = 0, k = 0, jR = 0, j = 0; i < row; i++) {
 
  
-             // 左边,上
 
 -             // j_l左边起始的指针
 
 -             // j总指针,从0到尽头16-1
 
 -             // k遍历列
 
 -             while (j < colunm2 - 1) {
 
 -                 for (k = 0; k < colunm2 - jL + 1; k++) {
 
 -                     while (jL < colunm2 - i) {
 
 -                         System.out.print("1");
 
 -                         jL++;
 
 -                         j++;
 
 -                     }
 
 -                     // jL已经完成使命,不再使用
 
 -                     j++;
 
 -                     System.out.print("2");
 
 -                 }
 
  
-             }
 
  
-             // 右边,下
 
 -             // jR右边起始为0的指针
 
 -             while (j >= colunm2 - 1 && j < colunm - 1) {
 
 -                 for (k = 0; k < colunm - j + 2;) {
 
 -                     while (jR < i + 1) {
 
 -                         System.out.print("3");
 
 -                         jR++;
 
 -                         j++;
 
 -                     }
 
 -                     j++;
 
 -                     System.out.print("4");
 
 -                 }
 
 -             }
 
  
-             // 每换一行,指针清零
 
 -             jL = 0;
 
 -             jR = 0;
 
 -             j = 0;
 
 -             System.out.println("");
 
 -             
 
 -             // i到达中间的时候
 
 -             if (i > row2 - 2 && i < row - 1) {
 
 -                 int row_D = row - row2;
 
 -                 down(row_D, colunm2, colunm);
 
 -                 break;
 
 -             }
 
 -         }
 
  
-     }
 
 -     //下半部分左边右边大结合
 
 -     private static void down(int row, int colunm2, int colunm) {
 
  
-         int b = 0;
 
 -         for (int i = 0, jL = 0, k = 0, jR = 0, j = 0; i < row; i++) {
 
 -         
 
 -             // 当处于右下左边时
 
 -             while (j < colunm2 - 1) {
 
 -                 for (k = 0; k < colunm2 - i; k++) {
 
 -                     while (jL < i + 1) {
 
 -                         System.out.print("5");
 
 -                         jL++;
 
 -                         j++;
 
 -                     }
 
 -                     j++;
 
 -                     System.out.print("6");
 
  
-                 }
 
 -             }
 
 -             
 
 -             // 当处于右下右边时
 
 -             while (j > colunm2 && j < colunm) {
 
 -                 for (k = 0; k < colunm - jR - row + 1; k++) {
 
 -                     while (jR < colunm2 - i) {
 
 -                         System.out.print("7");
 
 -                         j++;
 
 -                         jR++;
 
 -                     }
 
 -                     j++;
 
 -                     
 
 -                     System.out.print("8");
 
 -                 }                
 
 -             }
 
 -         
 
 -             // 每换一行,指针清零
 
 -             jL = 0;
 
 -             jR = 0;
 
 -             j = 0;
 
  
-             // 换行
 
 -             System.out.println("");            
 
 -         }
 
 -     }
 
  
- }
 
  
  复制代码- public class Test2 {
 
 -     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++){
 
 -             //如果大于最后一行-2,即到达最后一行-1,则按第2规律执行,否则按第1规律执行
 
 -             if(i > row-3){
 
 -                 for(j = i; j < colunm - i;j++){
 
 -                     a[i][j] = 1;
 
 -                 }
 
 -                 continue;
 
 -             }
 
 -             
 
 -             int head = 0;
 
  
-             //元素行与列位置相同,遍历列,遍历长度小于最右边元素长度
 
 -             for(j = i; j < colunm - i;j++){
 
 -                 
 
 -                 //当元素下标等于3,到达空白处;下标等于最右边元素位置-3,可以到达空白处
 
 -                 if(head == 2 || head == (colunm - 1) -2*i -2){
 
 -                     a[i][j] = 0;
 
 -                 }else{
 
 -                     a[i][j] = 1;
 
 -                 }
 
 -                 //前面下标++,换行时,重置为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();            
 
 -         }
 
 -     }
 
 - }
 
  
- //修改了一下代码,把图中end下标去除了,将head判断处改为head == (colunm - 1) -2*i -2
 
  复制代码 真心花了不少时间,不过还是在摸索中懂了很多东西 
for的联系到此为止了 
其实上面的方法并不是很好,效率可能还行,但是我觉得二维数组做起来会更简单 
当年在学校学C语言的时候,作业还是copy别人的,现在自己弄了一下其实也没太难,只是有些麻烦 
有感兴趣的,可以拿去参考,欢迎拍砖 
 
 
 
 |   
        
- 
1.jpg
(59.46 KB, 下载次数: 59)
 
 
 
 
- 
1.jpg
(59.55 KB, 下载次数: 49)
 
 
 
 
 
 
    
    
    
    
 
 
 |