break、continue、return

break

程序示例:

public class test1 {
    public static void main(String[] args) {
        /*需求:键盘录入一个大于等于2的整数 x ,计算并返回 x 的 平方根 。
        结果只保留整数部分 ,小数部分将被舍去 。*/
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个大于2的整数:");
        int input = sc.nextInt();
        for (int i = 1; i <= input; i++) {
            if (i * i == input) {
                System.out.println(i + "是" + input + "的平方根。");
                break;
            } else if (i * i > input) {
                System.out.println((i - 1) + "是" + input + "的平方根的整数部分。");
                break;
            }
        }
    }
}

执行结果:

请输入一个大于2的整数:9
3是9的平方根。
请输入一个大于2的整数:20
4是20的平方根的整数部分。

程序示例:

// 产生一个位于 [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) + " 次机会.");
        }
    }
}

程序示例:

// 需求:键盘录入一个正整数 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;
            }
        }
    }
}

程序示例:

public class test2 {
    public static void main(String[] args) {
        // 需求:程序自动生成一个1-100之间的随机数字,使用程序实现猜出这个数字是多少?
        // 扩展小需求:加一个保底机制,3次猜不中,直接提示猜中了。
        Random r = new Random();
        int num = r.nextInt(100) + 1;
        Scanner sc = new Scanner(System.in);
        System.out.print("请猜数字:");
        int guess = sc.nextInt();
        int count = 1;
        while (guess != num && count < 3) {
            if (guess > num) {
                System.out.println("猜大了,重新猜:");
                guess = sc.nextInt();
                ++count;
            } else if (guess < num) {
                System.out.println("猜小了,重新猜:");
                guess = sc.nextInt();
                ++count;
            }
        }
        if (guess == num || count >= 3) {
            System.out.println("猜中了");
        } else {
            System.out.println("没猜中");
        }
    }
}

另一种写法:

public class test2 {
    public static void main(String[] args) {
        // 需求:程序自动生成一个1-100之间的随机数字,使用程序实现猜出这个数字是多少?
        // 扩展小需求:加一个保底机制,3次猜不中,直接提示猜中了。
        Random r = new Random();
        int num = r.nextInt(100) + 1;
        int count = 0;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.print("请猜数字:");
            int guess = sc.nextInt();
            ++count;
            if (count == 3) {
                System.out.println("猜中了");
                break;
            }
            if (guess > num) {
                System.out.println("猜大了,重新猜");
            } else if (guess < num) {
                System.out.println("猜小了,重新猜");
            }
        }
    }
}

执行结果:

请猜数字:1
猜小了,重新猜
请猜数字:1
猜小了,重新猜
请猜数字:1
猜中了

对于 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
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值