黑马程序员技术交流社区

标题: for循环 [打印本页]

作者: X.MIKO    时间: 2015-7-7 23:30
标题: for循环
写出一个用for语句的镂空的等腰三角形
作者: 马利梅    时间: 2015-7-7 23:56
class VarDemo11 {          public static void main(String[] args) {              for(int x=1;x<=5;x++)                          {                                  for(int y=1;y<x;y++){                                  System.out.print(" ");                          }                          for(int z=x;z<=5;z++){                          System.out.print("* ");                          }                                         System.out.println( );}  }      }
作者: 马利梅    时间: 2015-7-7 23:57
class VarDemo11 {          public static void main(String[] args) {              for(int x=1;x<=5;x++)                          {                                  for(int y=1;y<x;y++){                                  System.out.print(" ");                          }                          for(int z=x;z<=5;z++){                          System.out.print("* ");                          }                                         System.out.println( );}  }      }
作者: Miss.H    时间: 2015-7-8 00:03
我这是正解
class ForForDemo
{
        public static void main(String[] args)
        {
               
                for (int x = 0;x<5;x++ )
                {
                        for (int y = x;y<5 ;y++ )
                        {
                                System.out.print(" ");
                        }
                        for (int z = 0; z<x+1;z++ )
                        {
                                System.out.print("* ");
                        }
                        System.out.println();
                }
        }
}
作者: 夜写意    时间: 2015-7-8 20:42
  1. int y = (x+1)/2;
  2. int count1 = (x+1)/2;
  3. int count2 = (x+1)/2;
  4. for(int i=0;i<y;i++){
  5.         for(int j=1;j<=x;j++){
  6.                 if(i==y-1)
  7.                         System.out.print("*");
  8.                
  9.                 else if((i!=y+1) && (j == count1 || j == count2))
  10.                         System.out.print("*");
  11.                
  12.                 else
  13.                         System.out.print(" ");                                       
  14.         }

  15.         count1++;
  16.         count2--;

  17.         System.out.println();
  18. }
复制代码

作者: 夜写意    时间: 2015-7-8 20:43
  1. static void DengYaoSanJiao(int x){
  2.         int y = (x+1)/2;
  3.         int count1 = (x+1)/2;
  4.         int count2 = (x+1)/2;
  5.         for(int i=0;i<y;i++){
  6.                 for(int j=1;j<=x;j++){
  7.                         if(i==y-1)
  8.                                 System.out.print("*");
  9.                        
  10.                         else if((i!=y+1) && (j == count1 || j == count2))
  11.                                 System.out.print("*");
  12.                        
  13.                         else
  14.                                 System.out.print(" ");                                       
  15.                 }

  16.                 count1++;
  17.                 count2--;

  18.                 System.out.println();
  19.         }
  20. }
复制代码

作者: 惜爱上缘    时间: 2015-7-8 22:53
/*
    *
   * *
  * * *
* * * *
* * * * *
*/

  public static void main(String[] args)
        {
                for (int i=0; i<5; i++)
                {
                        for (int j=i; j<4 ;j++)
                        {
                                System.out.print(" ");
                        }
                        for (int y=0; y<=i; y++)
                        {
                                System.out.print("* ");
                        }
                        System.out.println();
                }       
        }




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2