Jump statement in loop

break statement

Jump out from the branch statement or loop body where it is located, and execute the statement following the branch or loop body.

It is mostly used in two situations: ①Use a switch statement to terminate a case; ②Make a loop end immediately;

 

continue statement

Skip the remaining sentences of this round of loop and go directly to the next round of loop.

 

return statement

Make the program return from the method, and the method returns a value. If the return statement does not appear in the method, the last statement after the method is executed automatically returns to the program.

 

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;
    }
}

 

 

Guess you like

Origin blog.csdn.net/c_lanxiaofang/article/details/108929538