zoukankan      html  css  js  c++  java
  • java基础-控制语句

    1.条件语句if

    public class HelloWorld {
        public static void main(String[] args) {
            boolean b = false;
    
            // 如果有多个表达式,必须用大括弧包括起来
            if (b) {
                System.out.println("yes1");
                System.out.println("yes2");
                System.out.println("yes3");
            }
    
            // 否则表达式2 3 无论b是否为true都会执行
            if (b)
                System.out.println("yes1");
            System.out.println("yes2");
            System.out.println("yes3");
    
            // 如果只有一个表达式可以不用写括弧,看上去会简约一些
            if (b) {
                System.out.println("yes1");
            }
    
            if (b)
                System.out.println("yes1");
        }
    }
    public class HelloWorld {
        public static void main(String[] args) {
            int day = 5;
            
            // 如果只使用 if,会执行7次判断
            if (day == 1)
                System.out.println("星期一");
            if (day == 2)
                System.out.println("星期二");
            if (day == 3)
                System.out.println("星期三");
            if (day == 4)
                System.out.println("星期四");
            if (day == 5)
                System.out.println("星期五");
            if (day == 6)
                System.out.println("星期六");
            if (day == 7)
                System.out.println("星期天");
    
            // 如果使用else if, 一旦判断成立, 判断就不会执行了,节约了运算资源
            if (day == 1)
                System.out.println("星期一");
            else if (day == 2)
                System.out.println("星期二");
            else if (day == 3)
                System.out.println("星期三");
            else if (day == 4)
                System.out.println("星期四");
            else if (day == 5)
                System.out.println("星期五");
            else if (day == 6)
                System.out.println("星期六");
            else if (day == 7)
                System.out.println("星期天");
            else
                System.out.println("这个是什么鬼?");
        }
    }

    2.条件语句switch

    switch 语句相当于 if else

    public class HelloWorld {
        public static void main(String[] args) {
            int day = 5;
    
            // 如果使用switch相当于else if
            switch (day) {
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期天");
                break;
            default:
                System.out.println("这个是什么鬼?");
            }
        }
    }

    3.循环语句while和do-while

    public class HelloWorld {
        public static void main(String[] args) {
    
            // 打印0到4
            int i = 0;
            while (i < 5) {
                System.out.println("i" + i + "=" + i);
                i++;
            }
    
            // 打印0到4
            // 与while的区别是,无论是否成立,先执行一次,再进行判断
            int j = 0;
            do {
                System.out.println("j" + j + "=" + j);
                j++;
            } while (j < 5);
        }
    }

    4.循环语句for

    for循环,和while一样,只是表达方式不一样

    public class HelloWorld {
        public static void main(String[] args) {
            int i = 0;
    
            // 使用for打印0到4
            for (int j = 0; j < 5; j++) {
                System.out.println("for循环输出的 " + j);
            }
        }
    }

    5.continue

    continue语句的作用是跳过本次循环体中余下尚未执行的语句,立即进行下一次的循环条件判定,可以理解为仅结束本次循环。

    public class HelloWorld {
        public static void main(String[] args) {
    
            // 打印单数
            for (int j = 0; j < 10; j++) {
                if (0 == j % 2)
                    continue; // 如果是双数,后面的代码不执行,直接进行下一次循环
    
                System.out.println(j);
            }
        }
    }

    6.break

    用break语句可以使流程跳出switch语句体,也可以用break语句在循环结构终止本层循环体,从而提前结束本层循环。

    public class HelloWorld {
        public static void main(String[] args) {
    
            // 打印单数
            for (int j = 0; j < 10; j++) {
                if (0 == j % 2)
                    break; // 如果是双数,直接结束当前for循环
    
                System.out.println(j);
            }
        }
    }

    7.结束外部循环

    1.使用boolean变量结束外部循环

    public class HelloWorld {
        public static void main(String[] args) {
            boolean breakout = false; // 是否终止外部循环的标记
            for (int i = 1; i < 10; i++) {
                for (int j = 1; j < 10; j++) {
                    System.out.println("i=" + i + " : j=" + j + " breakout=" + breakout);
                    if (0 == j % 2) {
                        breakout = true; // 终止外部循环的标记设置为true
                        break;
                    }
                }
                if (breakout) // 判断是否终止外部循环
                    break;
            }
        }
    }

    2.使用标签结束外部循环

    public class HelloWorld {
        public static void main(String[] args) {
    
            // 打印单数
            outloop: // outloop这个标示是可以自定义的比如outloop1,ol2,out5
            for (int i = 1; i < 10; i++) {
    
                for (int j = 1; j < 10; j++) {
                    System.out.println("i=" + i + " : j=" + j);
                    if (0 == j % 2)
                        break outloop; // 如果是双数,结束外部循环
                }
            }
        }
    }

    8.练习

    1.小学算术题

    public class HelloWorld {
        /**
         * 提示使用多层循环嵌套解决
         *  口 + 口 = 8
         *  +    + 
         *  口 - 口 = 6
         *  =    = (往下等于)
         *  14   10
         * 
         * 求4个数字分别多少?
         */
        public static void main(String[] args) {
    
            // 已知范围 所有数相加不大于14
            for (int a = 0; a <= 14; a++) { // 设立数字a,假设是左上角
                int b = 8 - a; // 那么b 就是右上角
                int c = 14 - a; // c 就是左下角
                int d = 10 - b; // d 就是右下角(求 d 的其中一个方式)
                if (d == c - 6) { // 求 d 的另一个方式(两种都满足中和出最终值)
                    System.out.println("a=" + a + " b=" + b + " c=" + c + " d=" + d);
                }
            }
        }
    }

    2.九九乘法表

    public class HelloWorld {
        public static void main(String[] args) {
    
            // while实现九九乘法表
            System.out.println("==================while实现九九乘法表==================");
            int i = 0;
            while (i < 9) {
                i++;
                int j = 0;
                while (j < i) {
                    j++;
                    System.out.print(j + "*" + i + "=" + i * j);
                    System.out.print("	");
                }
                System.out.println();
            }
    
            // for实现九九乘法表
            System.out.println("==================for实现九九乘法表==================");
            for (int x = 1; x <= 9; x++) {
                for (int y = 1; y <= x; y++) {
                    System.out.print(y + "*" + x + "=" + x * y);
                    System.out.print("	");
                }
                System.out.println();
            }
        }
    }

    3.水仙花数

    水仙花数定义: 1. 一定是3位数 2. 每一位的立方,加起来恰好是这个数本身,比如153=1*1*1+5*5*5+3*3*3 寻找所有的水仙花数

    public class HelloWorld {
        public static void main(String[] args) {
         outloop:
    for (int baiwei = 1; baiwei <= 9; baiwei++) { for (int shiwei = 0; shiwei <= 9; shiwei++) { for (int gewei = 0; gewei <= 9; gewei++) { int num = baiwei * 100 + shiwei * 10 + gewei * 1; int opNum = gewei * gewei * gewei + shiwei * shiwei * shiwei + baiwei * baiwei * baiwei; if (num == opNum) System.out.println(num + "是水仙花数");
                if (baiwei == 9 && shiwei == 9 && gewei == 9) break outloop; } } } } }
    一辈子很短,努力的做好两件事就好;第一件事是热爱生活,好好的去爱身边的人;第二件事是努力学习,在工作中取得不一样的成绩,实现自己的价值,而不是仅仅为了赚钱。
  • 相关阅读:
    更换惠普G32笔记本的风扇和硬盘,内存条, 谨记 要做好CPU和显卡的 导热硅脂工作!
    怎么更新 WIN10里的SMBv1协议
    ubuntu-12.04.5-desktop-amd64 安装vmwaretools
    如何解决“ VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid.c:386 bugNr=1036521”
    联想移动硬盘无法访问 解决方法1
    阮一峰 ---开发者手册
    Earth Wind 一个查看全球风向的网站
    Linux帮助用法
    Linux历史命令管理以及用法
    Linux操作练习
  • 原文地址:https://www.cnblogs.com/antao/p/12820569.html
Copyright © 2011-2022 走看看