循环中的跳转语句

break语句

从它所在的分支语句或循环体中跳转出来,执行分支或循环体后面的语句。

多用在两种情况:①使用switch语句终止某个case;②使一个循环立即结束;

continue语句

跳过本轮循环剩余的语句,直接进入下一轮循环。

return语句

使程序从方法中返回,并未方法返回一个值。如果return语句未出现在方法中,则执行完方法后的最后一条语句自动返回到程序。

package com.temp;

import java.util.Scanner;

/**
 * @Author lanxiaofang
 * @email [email protected]
 * @date 2020/10/5 16:11
 */
public class AJumpStatementInALoop {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("请问您想循环几次?最少1次,最多100次");
        for(int i = Integer.parseInt(scanner.next()); ; i--){

            if(i < 1 || i > 100){
                System.out.println("不合法的次数");
                break;
            }
            System.out.println("这里是第"+i+"次的for循环");

            System.out.println("--请输入您喜欢的数字");
            switch (Integer.parseInt(scanner.next())){
                case 1 :
                    System.out.println("我");
                    break;
                case 2 :
                    System.out.println("ta");
                    continue;
                default:
                    System.out.println("你");
            }
            System.out.println("第 "+i+" 次的 switch 语句已结束");
        }
        System.out.println("for循环已结束");

        System.out.println("----- test is "+test());
    }

    public static int test(){
        return 0;
    }
}

猜你喜欢

转载自blog.csdn.net/c_lanxiaofang/article/details/108929538