黑马程序员技术交流社区

标题: 上次的代码完成了,没什么用但是逻辑性比较强 [打印本页]

作者: 梦魇0023    时间: 2016-9-24 22:03
标题: 上次的代码完成了,没什么用但是逻辑性比较强
共享一下,效果图如下:
两遍是循环, 中间是随机。
死循环

360反馈意见截图16581121115123113.png (30.81 KB, 下载次数: 7)

360反馈意见截图16581121115123113.png

作者: 梦魇0023    时间: 2016-9-24 22:04
代码如下
class Demo9 {
        public static void main (String[] args) {
                int a = 30 ;
                int b = 0 ;
                int e = 0;
                int n = 1;
                double d ;
                int r;
                int c = 7;
                boolean bl = true;
                boolean bt = true;
                while (bt) {       
                        n++;
                        if (n==0) {
                                bt= false;
                        }
                        d = Math.random();
                        r = (int)(d*10);
                        if (d > 0.5) {
                                a+=1;
                                if (a > 60) {
                                        a=60;
                                }
                        } else {
                                a-=1;
                                if (a < 1) {
                                        a = 1 ;
                                }
                        }
                        c++;
                        if (c == 7) {
                                bl = !bl;
                        }
                        c%=7;
                       
                        e = 7-c;

                       
                       
                        if (bl) {
                       
                                for (int c2 = 0; c2 < e ; c2++ ) {
                                        System.out.print(" ");       
                                }
                                System.out.print("*");
                                for (int c1 = 0; c1 < c ; c1++ ) {
                                        System.out.print(" ");
                                }
                        } else {
                               
                                for (int c1 = 0; c1 < c ; c1++ ) {
                                        System.out.print(" ");
                                }
                                System.out.print("*");
                                for (int c2 = 0; c2 < e ; c2++ ) {
                                        System.out.print(" ");       
                                }       
                        }
                       
                        System.out.print(r);
                        for ( int i = 0; i < a ; i++ ) {
                                System.out.print(" ");
                        }
                        System.out.print("*");
                        b = 60-a;
                        for ( int j = 0;j < b  ; j++ ) {
                                System.out.print(" ");
                        }
                        System.out.print(9-r);

                        if (bl) {               
                                for (int c1 = 0; c1 < c ; c1++ ) {
                                        System.out.print(" ");
                                }
                                System.out.println("*");
                        } else {
                                for (int c2 = 0; c2 < e ; c2++ ) {
                                        System.out.print(" ");       
                                }
                                System.out.println("*");
                        }
                                               
                }

        }
}
作者: 喝咖啡的玉米    时间: 2016-9-24 22:06
哇,好复杂,都运用了那方面的知识啊
作者: 梦魇0023    时间: 2016-9-24 22:09
喝咖啡的玉米 发表于 2016-9-24 22:06
哇,好复杂,都运用了那方面的知识啊

这几天学的知识。。for循环和if语句什么的
   自习课就干了这个…  




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