Java流程控制:顺序结构、选择结构、循环结构

Java流程控制:顺序结构、选择结构、循环结构

一、顺序结构

  • Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
  • 顺序结构是最简单的算法结构。
  • 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。

示例:

package com.ywj.Struct;

public class ShunXuDemo {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}
运行结果:
hello1
hello2
hello3
hello4
hello5

Process finished with exit code 0

二、选择结构

1、if单选择结构

  • 我们很多时候要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示。

  • 语法:

    if(布尔表达式){
        //如果布尔表达式为true将执行的语句
    }
    

    示例:

    package com.ywj.Struct;
    
    import java.util.Scanner;
    
    public class IfDemo01 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("请输入内容:");
            String s = scanner.nextLine();
    
            //equals:判断字符串是否相等
            if (s.equals("Hello")){
                System.out.println(s);
            }
            System.out.println("End");
            scanner.close();
        }
    }
    运行结果:
    请输入内容:
    lll
    End
    
    Process finished with exit code 0
    

2、if双选择结构

  • 语法

    if(布尔表达式){
        //如果布尔表达式的值为true
    }else{
        //如果布尔表达式的值为false
    }
    

    示例:

    package com.ywj.Struct;
    
    import java.util.Scanner;
    
    public class IfDemo2 {
        public static void main(String[] args) {
            //考试分数大于60就是及格,小于60就是不及格。
    
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("请输入成绩:");
            int score = scanner.nextInt();
    
            if (score>60){
                System.out.println("及格");
            }else {
                System.out.println("不及格");
            }
    
    
            scanner.close();
        }
    }
    运行结果:
    请输入成绩:
    88
    及格
    
    Process finished with exit code 0
    

3、if多选择结构

  • 语法:

    if(布尔表达式1){
        //如果布尔表达式1的值为true执行代码
    }else if{
        //如果布尔表达式2的值为true执行代码
    }else if{
        //如果布尔表达式3的值为true执行代码
    }else{
        //如果以上布尔表达式都不为true执行代码
    }
    

    示例:

    package com.ywj.Struct;
    
    import java.util.Scanner;
    
    public class IfDemo03 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("请输入成绩:");
            int score = scanner.nextInt();
    
            if (score==100){
                System.out.println("恭喜满分");
            }else if (score<100&&score>=90){
                System.out.println("优秀");
            }else if (score<90&&score>=80){
                System.out.println("好");
            }else if (score<80&&score>70){
                System.out.println("良好");
            }else if (score<70&&score>=60){
                System.out.println("及格");
            }else if (score<60&&score>=0){
                System.out.println("不及格");
            }else{
                System.out.println("您的成绩不合规范!");
            }
            scanner.close();
        }
    }
    运行结果:
    请输入成绩:
    88Process finished with exit code 0
    

4、嵌套的if结构

  • 使用嵌套的if…else语法是合法的。也就是说你可以在另一个if或者else if 语句中使用if或者else if语句。你可以想if语句一样嵌套else if…else。

  • 语法:

    if(布尔表达式1){
        //如果布尔表达式1的值为true执行代码
        if(布尔表达式2){
            //如果布尔表达式2的值为true执行代码
        }
    }
    

    示例:

    package com.ywj.Struct;
    
    public class IfDemo04 {
        /** 已知三个整数,求其中最大的值*/
        public static void main(String[] args) {
            int a = 15;
            int b = 7;
            int c = 21;
            if (a>b){
                if (a>c){
                    System.out.println("a = "+a);
                }else{
                    System.out.println("c = "+c);
                }
            }else{
                if (b>c){
                    System.out.println("b = "+b);
                }else{
                    System.out.println("c = "+c);
                }
            }
        }
    }
    运行结果:
    c = 21
    
    Process finished with exit code 0
    
    

5、switch多选择结构

  • 多选择结构还有一个实现方式就是switch case语句。

  • switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

  • switch语句中的变量类型可以是:

    • byte、short、int或者char。
    • 从Java SE 7开始
    • 从switch支持字符串String类型了
    • 同时case标签必须为字符串常量或字面量。
  • 语法

    switch(expression){
        case value:
            //语句
            break;//可选
        case value:
            //语句
            break;//可选
           //你可以有任意数量的case语句
        default: //可选
            //语句
    }
    

    示例:

    package com.ywj.Struct;
    
    public class SwitchDemo01 {
        public static void main(String[] args) {
            //case穿透 //switch匹配一个具体的值
            char grade = 'C';
    
            switch (grade){
                case 'A':
                    System.out.println("优秀");
                    break;//可选
                case 'B':
                    System.out.println("良好");
                    break;//可选
                case 'C':
                    System.out.println("及格");
                    break;//可选
                case 'D':
                    System.out.println("再接再厉");
                    break;//可选
                case 'E':
                    System.out.println("挂科");
                    break;//可选
                    default:
                        System.out.println("未知等级");
            }
        }
    }
    运行结果:
    及格
    
    Process finished with exit code 0
    
    

    示例2

    package com.ywj.Struct;
    
    public class SwitchDemo02 {
        public static void main(String[] args) {
            String name = "Yj";
            //JDK7的新特性,表达式结果可以是字符串!!!
            //字符的本质还是数字
    
            //反编译  java---class(字节码文件)---反编译(IDEA)
            switch (name){
                case "peter":
                    System.out.println("peter");
                    break;
                case "Yj":
                    System.out.println("Yj");
                    break;
                default:
                    System.out.println("KIKI");
            }
        }
    }
    运行结果:
    Yj
    
    Process finished with exit code 0
    

三、循环结构

  • while循环

    • while是最基本的循环,它的结构为:

      while(布尔表达式){
      //循环内容
      }
      
    • 只要布尔表达式为true,循环就会一直执行下去。

    • 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。

    • 少部分情况需要循环一直执行,比如服务器的请求响应监听等。

    • 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!

      示例:

      package com.ywj.Struct;
      
      public class WhileDemo01 {
          public static void main(String[] args) {
      
              //输出1-100
      
              int i = 0;
      
              while (i<100){
                  i++;
                  System.out.println(i);
              }
      
      
          }
      }
      
      package com.ywj.Struct;
      
      public class WhileDemo02 {
          public static void main(String[] args) {
              //z计算1+2+3+...+100=?
      
              //
      
              int i = 0;
              int sum = 0;
      
              while (i<=100){
                  sum = sum + i;
                  i++;
              }
      
              System.out.println(sum);
      
          }
      }
      
      
      
  • do…while循环

    • 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。

    • do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。

      语法:

      do{
          //代码语句
      }while(布尔表达式);
      
    • while和do-while的区别:

      • while先判断后执行。do-while是先执行后判断!
      • Do…while总是保证循环体会被至少执行一次!这是他们的主要区别。

    示例:

    package com.ywj.Struct;
    
    public class DoWhileDemo03 {
        public static void main(String[] args) {
            //do while语句和while语句的比较
            int a=10;
            int b=10;
            while (a<10){
                a++;
            }
            System.out.println("a="+a);
            do {
                b++;
            }while (b<10);
            System.out.println("b="+b);
    
        }
    }
    
    
  • for循环

    • 虽然所有循环结构都可以用while或者do…while表示,但Java提供了另一种语句——for循环,使一些循环结构变得更加简单。
    • for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
    • for循环执行的次数是在执行前就确定的。

    语法格式如下:

    for(初始化;布尔表达式;更新){
        //代码语句
    }
    

    示例

    package com.ywj.Struct;
    
    public class ForDemo04 {
        public static void main(String[] args) {
            //打印99乘法表
            //1.打印第一列
            //2.把固定的1再用一个循环包起来
            //3.去掉重复项,i<=j
            //4.调整样式
            for (int j = 1; j <=9; j++) {
                for (int i = 1; i <= j ; i++) {
                    System.out.print(j+"*"+i+"="+(j*i)+"\t");
                }
                System.out.println();
            }
        }
    }
    运行结果
    1*1=1	
    2*1=2	2*2=4	
    3*1=3	3*2=6	3*3=9	
    4*1=4	4*2=8	4*3=12	4*4=16	
    5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
    6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
    7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
    8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
    9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	
    
    Process finished with exit code 0
    
    
  • 增强型for循环。

    • Java增强型for循环语法格式如下:

      for(声明语句:表达式){
          //代码句子
      }
      
    • 声明语句:声明新的局部变量,改变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相当。

    • 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

示例:

package com.ywj.Struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义了一个数组

        for (int i = 0;i<5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("===================");

        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}
运行结果:
10
20
30
40
50
===================
10
20
30
40
50

Process finished with exit code 0    

break&continue

  • break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
  • continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
  • 关于goto关键字
    • goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用;Java没有goto。然而,在break和continue这两个关键字的身上,我们仍能看出一些goto的影子—带标签的break和continue。
    • "标签"是指后面跟一个冒号的标识符,例如:label:
    • 对Java来说唯一用到标签的地方是在循环语句之前。而循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。

示例:

package com.ywj.Struct;

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

        System.out.println("123");
    }
}
package com.ywj.Struct;

public class ContinueDemo01 {
    //continue
    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);
        }
        //break在任何循环语句的主体部分,均可用break控制循环的流程。
        //break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
        //continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。


    }
}

package com.ywj.Struct;

public class LabelDemo01 {
    public static void main(String[] args) {
        //label
        //打印101-150之间所有的质数

        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.println(i+"");
        }



    }
}


练习

package com.ywj.Struct;

public class TestDemo {
    public static void main(String[] args) {
        //打印三角形  5行

        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();
        }
    }
}
运行结果:
     *
    ***
   *****
  *******
 *********

Process finished with exit code 0
    
打印爱心:
package com.ywj.Struct;

public class TestDemo01 {
    public static void main(String[] args) {
        // 分三个大部分 上中下
        for (int i = 0, k = 0; i < 14; i++) {// 打印行
            // 上部分 上分为 四个部分
            if (i < 3) {
                for (int j = 0; j < 5 - 2 * i; j++) {// 1、空心
                    System.out.print(" ");
                }
                if (i == 2) {// 2、*
                    for (int j = 0; j < 6 + 4 * i - 1; j++) {
                        System.out.print("*");
                    }
                    for (int j = 0; j < 7 - 4 * i + 2; j++) {// 3、空心
                        System.out.print(" ");
                    }
                    for (int j = 0; j < 6 + 4 * i - 1; j++) {// 4、*
                        System.out.print("*");
                    }
                } else {
                    for (int j = 0; j < 6 + 4 * i; j++) {// 2、*
                        System.out.print("*");
                    }
                    for (int j = 0; j < 7 - 4 * i; j++) {// 3、空心
                        System.out.print(" ");
                    }
                    for (int j = 0; j < 6 + 4 * i; j++) {// 4、*
                        System.out.print("*");
                    }
                }
            } else if (i < 6) {// 中间
                for (int j = 0; j < 29; j++) {
                    System.out.print("*");
                }
            } else {// 下部分 6
                if (i == 13) {
                    for (int j = 0; j < 2 * (i - 6); j++) {// 打印空格
                        System.out.print(" ");
                    }
                    System.out.print("*");
                } else {
                    for (int j = 0; j < 2 * (i - 6) + 1; j++) {// 打印空格

                        System.out.print(" ");
                    }
                    for (int j = 1; j < 28 - 4 * k; j++) {

                        System.out.print("*");
                    }
                    k++;
                }
            }
            System.out.println();// 换行
        }
    }
}
运行结果:
     ******       ******
   **********   **********
 ************* *************
*****************************
*****************************
*****************************
 ***************************
   ***********************
     *******************
       ***************
         ***********
           *******
             ***
              *

Process finished with exit code 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值