A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

class Test
{
        public static void main(String[] args)
        {
                for(int x=0; x<4; x++)
                {
                        for(int y=0; y<6; y++)
                        {
                                System.out.print("*");
                        }
                        System.out.println();
                }
                System.out.println("-------------------");

                /*
               
                *****
                ****
                ***
                **
                *
       

                */
               
               
                for (int x=0; x<8 ;x++ )
                {
                        for (int y=x; y<8 ;y++)
                        {
                                System.out.print("*");
                        }
                        System.out.println();
                       
                }
        }
}

12 个回复

倒序浏览
int x = 0;
回复 使用道具 举报
int x = 0;
int y = 0;
while(x<4){
        while(y<6){
                System.out.print("*");
                 y++;
        }
        System.out.println();
        x++;
}

点评

这是打印方形的做法吧,但如果是打印三角形的做法呢?  发表于 2016-1-10 11:08
回复 使用道具 举报
                int x = 0;
                int y = 0;
                while(x<4){
                    while(y<=x){
                       System.out.print("*");
                       y++;
                    }
                    System.out.println();
                    x++;
                    y=0;
                }
回复 使用道具 举报
本帖最后由 Devilbaby 于 2016-1-10 22:38 编辑

int a=0;
                 while(a<8){
                         int b = a;
                         while(b<8){
                                 System.out.print("*");
                                 b++;
                         }
                         System.out.println();
                         a++;
                 }

QQ图片20160110223728.png (78.44 KB, 下载次数: 27)

QQ图片20160110223728.png
回复 使用道具 举报
Devilbaby 发表于 2016-1-10 22:35
int a=0;
                 while(a

这个一般都会选择用for做  比较方便
回复 使用道具 举报
Tkight 中级黑马 2016-1-11 09:45:24
7#
while还能这样做。。
回复 使用道具 举报
好吧,长知识,while循环第一次见这样用,平时都用for,原来while也可以这样嵌套
回复 使用道具 举报
萌萌看不懂
回复 使用道具 举报
不过对于内存的优化来说,for更占优势
回复 使用道具 举报
1066715808 发表于 2016-1-15 15:50
不过对于内存的优化来说,for更占优势

感觉for也更好用啊,不知道while好在哪里?

点评

两者各有优势,for运行完就自动清除内存,while则一直保持; while用true参数可以一直保持循环,而for不能或有限制  发表于 2016-1-19 10:42
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马