break、continue、return

break

程序示例:

// 产生一个位于 [1, 100] 范围内的随机数,统计产生 100 所需要的次数
public static void main(String[] args) {
    // System.out.println(Math.random());  // [0,1)
    // System.out.println(Math.random() * 100);  // [0,100)
    // System.out.println((int) (Math.random() * 100));  // [0,99]
    // System.out.println((int) (Math.random() * 100) + 1);  // [1,100]
    int count = 0;
    while (true) {
        int a = (int) (Math.random() * 100) + 1;
        System.out.println(a);
        ++count;
        if (a == 100)
            break;
    }
    System.out.println(count);
}

对于位于循环内部的 break,默认情况下都是结束当前最近位置的循环,但是可以用标签修改这一默认设置,明确指定想要结束的循环。如果外部循环结束了,那么内部循环当然也就结束了。

程序示例:

public static void main(String[] args) {
    abc1:
    for (int i = 1; i <= 10; i++) {
        abc2:
        for (int j = 0; j <= 10; j++) {
            if (j == 2)
                break abc1;    //结束外层 for
            System.out.println(j);
        }
    }
}

执行结果:

0
1

程序示例:

public static void main(String[] args) {
    int n = 0;
    int sum = 0;
    for (int i = 1; i <= 100; i++) {
        sum += i;
        if (sum >= 20) {
            n = i;
            break;
        }
    }
    System.out.println("sum = " + sum);
    System.out.println("n = " + n);     // 如果在定义 n 时不赋值初始值,则此处会报错:可能尚未初始化变量 n
}

执行结果:

sum = 21
n = 6

程序示例:

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String name;
        String ps;
        for (int chance = 3; chance > 0; chance--) {
            System.out.println("输入姓名:");
            name = sc.next();
            System.out.println("输入密码:");
            ps = sc.next();
            if ("小李".equals(name) && "899".equals(ps)) {
                System.out.println("登录成功");
                break;
            }
            System.out.println("输入错误,还有 " + (chance - 1) + " 次机会.");
        }
    }
}

程序示例:

// 求平方根
// 需求:键盘录入一个大于等于 2 的整数 ×,计算并返回 x 的平方根。
// 结果只保留整数部分,小数部分将被舍去。

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        int number = sc.nextInt();
        for (int i = 1; i <= number; i++) {
            if (i * i == number) {
                System.out.println(i + "是" + number + "的平方根");
                break;
            } else if (i * i > number) {
                System.out.println((i - 1) + "是" + number + "的平方根的整数部分");
                break;
            }
        }
    }
}

程序示例:

// 需求:键盘录入一个正整数 x,判断该整数是否为一个质数。

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        System.out.print("请输入一个正整数:");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        boolean flag = true;
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                flag = false;
                break;
            }
        }
        if (flag) {
            System.out.println(number + "是质数。");
        } else System.out.println(number + "不是质数。");
    }
}

程序示例:

// 需求:程序自动生成一个 [1, 100] 之间的随机数字,使用程序实现猜出这个数字是多少?

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        int random_number = (int) (Math.random() * 100 + 1);
        System.out.print("请猜这个数是多少:");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        while (number != random_number) {
            if (number < random_number) {
                System.out.print("猜小了,再来:");
                number = sc.nextInt();
            } else {
                System.out.print("猜大了,再来:");
                number = sc.nextInt();
            }
        }
        System.out.println("猜对了");
    }
}

执行结果:

请猜这个数是多少:50
猜小了,再来:75
猜小了,再来:85
猜小了,再来:90
猜小了,再来:95
猜小了,再来:97
猜大了,再来:96
猜对了

另一种写法:

// 需求:程序自动生成一个 [1, 100] 之间的随机数字,使用程序实现猜出这个数字是多少?

import java.util.Random;
import java.util.Scanner;

import java.util.Random;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        // 利用 Random 类获取随机数
        Random r = new Random();
        // 左闭右开区间
        int random_number = r.nextInt(100) + 1;
        // 生成任意范围内的值的方法:
        // 1. 左右区间都减去左区间
        // 2. 右区间加 1
        // 3. 这个右区间的值,就是 nextInt() 里面要输进去的值
        // 4. 将 nextInt() 外面加原来的左区间
        // 比如,要生成 [7, 15]
        // 先减去左区间,得到 [0, 8]
        // 右区间加 1,得到 [0, 9]
        // 最终的写法为 r.nextInt(9) + 7
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("猜这个数字是多少:");
            int guessNum = sc.nextInt();
            if (guessNum > random_number)
                System.out.println("猜大了,再来。");
            else if (guessNum < random_number)
                System.out.println("猜小了,再来。");
            else {
                System.out.println("猜中了");
                break;
            }
        }
    }
}

对于 for 循环,break 结束后,直接执行 for 循环后面的语句,不会执行 for 循环头中的表达式 3。对于 while 循环,break 结束后,执行 while 循环后面的语句。

continue

程序示例:

public class Continue {
    public static void main(String[] args) {
        abc1:
        for (int i = 0; i < 2; i++) {
            abc2:
            for (int j = 0; j < 5; j++) {
                if (j == 2)
                    // continue; //等价于continue abc2;
                    continue abc1;  // 结束外层 for 循环
                System.out.println(j);
            }
        }
    }
}

执行结果:

0
1
0
1

对于 for 循环,continue 结束后,执行 for 循环头中的表达式 3,再执行判断条件,判断是否继续循环。对于 while 循环,continue 结束后,执行 while 循环头的判断表达式。

return

return 多使用在方法中,表示跳出所在的方法。

如果 return 写在 main 中,表示退出程序。

程序示例:

public class Return {
    public static void main(String[] args) {
        for (int i = 0; i <= 10; i++) {
            if (i == 3) {
                System.out.println(i);
                return;
            }
            System.out.println("ok" + i);
        }
        System.out.println("hello");
    }
}

执行结果:

ok0
ok1
ok2
3

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 19:54:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 19:54:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 19:54:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 19:54:05       20 阅读

热门阅读

  1. nginx location正则表达式+案例解析

    2024-06-08 19:54:05       13 阅读
  2. 设计模式-外观模式

    2024-06-08 19:54:05       9 阅读
  3. mysql怎么部署双机

    2024-06-08 19:54:05       8 阅读
  4. Percona XtraBackup备份用户需要的最小权限(mysql)

    2024-06-08 19:54:05       12 阅读
  5. 基于网格的CLIQUE聚类算法的实现

    2024-06-08 19:54:05       9 阅读
  6. Python | 开房门(map)

    2024-06-08 19:54:05       6 阅读
  7. hash和history路由模式的区别

    2024-06-08 19:54:05       7 阅读
  8. 深入Vue.js:从基础到进阶的全面学习指南

    2024-06-08 19:54:05       11 阅读
  9. 2024年BCSP-X小学高年级组初赛真题解析

    2024-06-08 19:54:05       11 阅读