程序的流程控制一般分为3种:

  • 顺序结构:就是不加任何控制,代码从main方法开始自上而下执行

  • 分支结构:就是根据条件判断是true还是false,有选择性的执行哪些代码。在Java语言中提供了两个格式if 、 switch

  • 循环结构:就是控制某一段代码重复执行。在Java语言中提供了三种格式,for、while、do-while

分支结构

if分支

if它的作用,是用于对条件进行判断,判断的结果只可能有两个值true或者false,然后根据条件判断的结果来决定执行那段代码。


public class Demo {

    public static void main(String[] args) {

        int score = 298;

        if (score >= 0 && score < 60) {

            System.out.println("您的绩效级别是: D");

        } else if (score >= 60 && score < 80) {

            System.out.println("您的绩效级别是: C");

        } else if (score >= 80 && score < 90) {

            System.out.println("您的绩效级别是: B");

        } else if (score >= 90 && score <= 100) {

            System.out.println("您的绩效级别是: A");

        } else {

            System.out.println("您录入的分数有毛病~~");

        }

    }

}

switch分支

switch 分支的作用,是通过比较值来决定执行哪条分支代码


public class Demo {

    public static void main(String[] args) {

        String week = "周三";

        switch (week) {

            case "周一":

                System.out.println("埋头苦干,解决bug");

                break;

            case "周二":

                System.out.println("请求大牛程序员帮忙");

                break;

            case "周三":

                System.out.println("今晚啤酒、龙虾、小烧烤");

                break;

            case "周四":

                System.out.println("主动帮助新来的女程序解决bug");

                break;

            case "周五":

                System.out.println("今晚吃鸡");

                break;

            case "周六":

                System.out.println("与王婆介绍的小芳相亲");

                break;

            case "周日":

                System.out.println("郁郁寡欢、准备上班");

                break;

            default:

                System.out.println("您输入的星期信息不存在~~~");

        }

    }

}

如果是对一个范围进行判断,建议使用if分支结构;如果是与一个一个的值比较的时候,建议使用switch分支结构

switch 注意事项

  1. 表达式类型只能是byte、short、int、char JDK5开始支持枚举,JDK7开始支持String 不支持double、float、double

  2. case给出的值不允许重复,且只能是字面量,不能是变量。

  3. 正常使用switch的时候,不要忘记写break,否则会出现穿透现象。

循环结构

for循环


// 求1~100中所有整数的和

public class Demo {

    public static void main(String[] args) {

        int sum = 0;

        // 定义一个循环,先产生1-100,这100个数

        for (int i = 1; i <= 100; i++) {

            //每产生一个数据,就把这个数和sum累加

            sum += i; //sum = sum  + i;

        }

        System.out.println("1-100的数据和:" + sum);

    }

}


// 求1~100之间奇数的和

public class Demo {

    public static void main(String[] args) {

        int sum1 = 0;

        for (int i = 1; i < 100; i += 2) {

            sum1 += i;

        }

        System.out.println("1-100之间的奇数和:" + sum1);

    }

}

while循环


public class Demo {

    public static void main(String[] args) {

        int i = 0;

        while (i < 5) {

            System.out.println("Hello World");

            i++;

        }

    }

}

for、while如何选择

  • 从功能来说:能够用for循环做的,都能用while循环做。

  • 使用规范上来说:知道循环几次,建议使用for;不知道循环几次建议使用while

do-while

do-while循环的特点是先执行,再判断的。即使条件不成立,也会先执行一次。

  1. for循环 和 while循环(先判断后执行); do...while (先执行后判断)

  2. for循环和while循环的执行流程是一模一样的,功能上无区别,for能做的while也能做,反之亦然。如果已知循环次数建议使用for循环,如果不清楚要循环多少次建议使用while循环

  3. for循环中控制循环的变量只在循环中使用; while循环中,控制循环的变量在循环后还可以继续使用

死循环


public class Demo {

    public static void main(String[] args) {

        for (; ; ) {

            System.out.println("Hello World1");

        }



        // while死循环

        while (true) {

            System.out.println("Hello World2");

        }



        // do-while死循环

        do {

            System.out.println("Hello World3");

        } while (true);

    }

}

循环嵌套

外部循环每循环一次,内部循环会全部执行完一轮。


public class Demo {

    public static void main(String[] args) {

        for (int i = 1; i <= 4; i++) {

            for (int j = 1; j <= 5; j++) {

                System.out.print("*"); // 不换行

            }

            System.out.println(); //换行

        }

    }

}

跳转语句

跳转语句,需要用到breakcontinue两个关键字。

  • break作用:跳出并结束当前所在循环的执行

  • continue作用:结束本次循环,进入下一次循环

案例

生成随机数

生成随机数的功能,其实 Java已经给我们提供了,在JDK中提供了一个类叫做Random,我们只需要调用Random这个类提供的功能就可以了。


import java.util.Random;



public class RandomDemo {

    public static void main(String[] args) {

        Random r = new Random();

        for (int i = 1; i <= 20; i++) {

            int data = r.nextInt(10);

            System.out.println(data);

        }

    }

}

猜数字小游戏

需求:随机生成一个1-100之间的数据,提示用户猜测,猜大提示过大,猜小提示过小,直到猜中 结束游戏


import java.util.Random;

import java.util.Scanner;



public class RandomTest {

    public static void main(String[] args) {

        Random r = new Random();

        int luckNumber = r.nextInt(100) + 1;



        Scanner sc = new Scanner(System.in);

        while (true) {

            System.out.println("请您输入一个数字:");

            int guessNumber = sc.nextInt();



            if (guessNumber > luckNumber) {

                System.out.println("您猜测的数字过大");

            } else if (guessNumber < luckNumber) {

                System.out.println("您猜测的数字过小");

            } else {

                System.out.println("恭喜你猜对了");

                break; // 结束死循环

            }

        }

    }

}