流程控制-break& continue

流程控制-break& continue

break

break在任何循环语句的主体部分,均可用 break控制循环的流程。 break用于强行退出循环,不执行循环中剩余的语句。( break语句也在 switch语句中使用)。

continue

continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

关于goto关键字

  • goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用;Java没有goto。然而,在 breaki和 continue这两个关键字的身上,我们仍然能看出一些goto的影子—带标签的 break和continue。
  • “标签”是指后面跟一个冒号的标识符,例如:label;
  • 对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于 break和 continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。

break

package com.gcbeen.struct;

/**
 * @author gcbeen 
 */
public class BreakDemo01 {

    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            i++;
            System.out.println(i);
            if (i == 30) {
                break;
            }
        }
        System.out.println("123");
    }

}

continue

package com.gcbeen.struct;

/**
 * @author gcbeen 
 */
public class ContinueDemo {

    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            i++;
            if (i % 10 == 0) {
                System.out.println();
                continue;
            }
            System.out.println(i);
        }
    }

}

标签

package com.gcbeen.struct;

/**
 * @author gcbeen 
 */
public class LabelDemo {

    public static void main(String[] args) {
        // 打印101~150之间的所有质数
        // 质数是指在大的自然数中,除1和它本身以外不再有其他因数的自然数。
        int count = 0;
        outer : for (int i = 101; i < 150; i++) {
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0) {
                    continue outer;
                }
            }
            System.out.print(i + " ");
        }
    }

}

打印三角形

package com.gcbeen.struct;

/**
 * @author gcbeen 
 */
public class TestDemo01 {

    // 打印三角形
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            // 左边空白倒直角
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            // 左边星型直角
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            // 右边星型直角
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            // 右边空白倒直角不用打印
            System.out.println();
        }
    }

}


/*输出效果:
     *
    ***
   *****
  *******
 *********
*/

写一个计算器

要求实现加减乘除功能,井且能够循环接收新的数据,通过用户交互实现。

package com.gcbeen.struct;

import java.util.Scanner;

/**
 * @author gcbeen
 * 
 */
public class HomeWork {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double sum = 0;
        while (true) {
            System.out.println("请输入第一个整数:");
            double a = scanner.nextDouble();
            System.out.println("请输入第二个整数:");
            double b = scanner.nextDouble();
            System.out.println("请输入要运算的字符(+:表示加法,-:表示减法,*:表示乘法,/:表示除法)");
            String temp = scanner.next();

            switch (temp) {
                case "+":
                    sum = a + b;
                    System.out.println("结果是:" + sum);
                    continue;
                case "-":
                    sum = a - b;
                    System.out.println("结果是:" + sum);
                    continue;
                case "*":
                    sum = a * b;
                    System.out.println("结果是:" + sum);
                    continue;
                case "/":
                    sum = a / b;
                    System.out.println("结果是:" + sum);
                    continue;
                default:
                    System.out.println("请输入正确的运算符号!!");
            }
        }
    }
}
posted @ 2022-09-09 09:54  gcbeen  阅读(43)  评论(0)    收藏  举报