0 0

如何使用JAVA一层for循环打印出倒立三角形? ********* ******* ***** *** *10

如何使用JAVA一层for循环打印出倒立三角形?
*********
*******
  *****
   ***
    *

要求给出算法实现,怎么控制的

问题补充:
高级java工程师 写道
    for (int i = 0; i < m; i++) {
          if((m-i)%2==0)
             System.out.println("");
          else{
          for(int k=0;k<i;k=k+2){
           System.out.print(" ");
          }
             for(int j=m;j>i;j--){
              System.out.print("*");
             }
          }
   }
  

    
    return 0;
   }



你这是两层循环的吧
 
2012年3月12日 22:02

4个答案 按时间排序 按投票排序

0 0

        int i = 0;
        int j = 0;
        int row = 10;//行数
        int col = 10;//列数
        while (true) {
            if (i > row && j > col) {
                break;//超出边界跳出
            }
            if (i < row && j > col) {  //如果列超出边界,行没超出,则换行
                System.out.println("");
                j = 0;//列从0开始重新计数
				i++;
				continue;

            } 
			if (i < row && j < col) {//行列都在边界内
                if (j >i && j < row - i) {
                    System.out.print("*");//如果列大于等于行并且,小于边界-行时画*
                } else {
                    System.out.print(" ");
                }
            }

            j++;

        }

2012年3月15日 22:40
0 0

	static void print(int m) {
		int max = 2*m-1;//正方形边长。
		int len = max * max;//正方形中的格子数
		int row = 1 ;//行数
		int left = 0;//左边起始位置的前一个位置。
		int right = max;//右边终止位置
		int count = 0;//第row行的星号个数
		int total = max* row;
		for (int i = 1; i <= len; i++) {		
			if(left < i && i <= right  ){
				System.out.print("*");
			} else {
				System.out.print(" ");
			}			
			if( i == total) {
				System.out.println();
				++row;
				count = (max - ((m+1 -row)*2-1))/2;
				total = max* row;
				left  = total - max +count;// max * (row-1)+ count;
				right = total-count;
			}
		}
	}


结果:
print(5);
*********
 ******* 
  *****  
   ***   
    *    

2012年3月14日 19:03
0 0

public class Test {

public static void main(String[] args) {
  
   int row = 0;//行
   int col = -5;//列
  
   /*
            row
    -5-4-3-2-1 * 1 2 3 4 5     0 
    -5-4-3-2 *   *   * 2 3 4 5     1
    -5-4-3 *   *   *   *   * 3 4 5     2
    -5-4 *   *   *   *   *   *   * 4 5     3
    -5 *   *   *   *   *   *   *   *   * 5     4
   
   row -5-4-3-2-1 0 1 2 3 4 5  
  
   */
   while(true){
   
    if(col>=-row&&col<=row){
   
     System.out.print("* ");
    
    }else{
    
     System.out.print("   ");
    
    }
   
    if(col>row){
    
     row++;//移到下一行
    
     col=-5;//初始化列
    
     System.out.println();
    
     continue;//跳转到循环开始处
    }
   
    col++;
   
    if(col==5){//到第5行就跳出循环
     System.out.println();
     break ;
    }
   }
} 
}

2012年3月12日 22:18
0 0

java打印倒三角形 
/*下面是一个由*号组成的4行倒三角形图案。
要求:1、输入倒三角形的行数,
行数的取值3-21之间,对于非法的行数,
要求抛出提示“非法行数!”;2、在屏幕上打印这个指定了行数的倒三角形。
    *******
     *****
      ***
       *
*/

package 字符处理;

public class 打印倒三角形 {

   public static int printS(int m){
    if(m>21||m<3)
     return 1;
    
    for (int i = 0; i < m; i++) {
          if((m-i)%2==0)
             System.out.println("");
          else{
          for(int k=0;k<i;k=k+2){
           System.out.print(" ");
          }
             for(int j=m;j>i;j--){
              System.out.print("*");
             }
          }
   }
  

    
    return 0;
   }
public static void main(String[] args) {
      int s= printS(10);
      if(s==1)
      System.out.println("非法行数!");
}

}

2012年3月12日 22:03

相关推荐

Global site tag (gtag.js) - Google Analytics