顺序结构基础

顺序结构

  • Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行

  • 顺序结构就是最简单的结构

  • 语句与语句之间,框与框之间是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的基本算法结构

    package com.sgz.structure;
    
    public class Turn {
        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
    

选择结构

if单选择结构

  • 当我们需要判断一个东西是否可行,可行才去执行下一步时,就需要一个过程在程序中的if语句来表示

  • 语法格式

    if(布尔值表达式==true){
        //==true可以省略,意为 如果布尔表达式为true将执行里面的语句
    }
    
    package com.sgz.structure;
    
    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")){//判断s是不是等于Hello
                System.out.println(s);
    
            }
              System.out.println("End");
            scanner.close();
        }
    }
    
    

    image-20220715165324825

image-20220715165546864


if双选择结构

  • 如果成功就输出a,不行就输出b (只能在两个里选择输出)

  • 语法格式

    if(布尔值表达式){
        //如果布尔表达式的值为true,执行这里面的程序
    }else{
        //如果布尔值表达式的值为false,执行这里面的程序
    }
    
    public class IfDemo02 {
        //考试分数大于60及格,小于60不及格
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入成绩:" );
            double score = scanner.nextDouble();
            if (score>=60){
                System.out.println("合格");
            }else if(score>=0,score<60){
                System.out.println("不合格");
            }else{
                System.out.println("输入内容不合法!")//缜密一些
            }
            scanner.close();
        }
    }
    

if多选择结构

  • 如果第一个判断成功则输出a,如果第二个判断成功则输出b,依此类推,如果上面都判断不成功则输出c

  • 语法结构

    if(布尔值表达式1){
        //如果布尔值表达式1的值为true执行这里面的代码
    }else if(布尔值表达式2){
        //如果布尔值表达式2的值为true执行这里面的代码
    }else if(布尔值表达式3){
        //如果布尔值表达式3的值为true执行这里面的代码
    }else{
        //如果以上布尔值表达式都不为true执行这里面的代码
    }
    
    • 注意点
      1. if语句至多有一个else语句,else语句必须在所有的else if语句之后
      2. if语句可以有若干个else if 语句,但他们必须都在else语句之前
      3. else语句检测为true,其他所有语句都跳过执行
      4. else语句检测为false后,一旦有一个else if 语句检测为true,其他else if 和 else 语句都将跳过执行
      5. 注意区间问题
    package com.sgz.structure;
    
    import java.util.Scanner;
    
    public class IfDemo03 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入成绩:" );
            double score = scanner.nextDouble();
             /*注意点
            1.if语句至多有一个else语句,else语句必须在所有的else if语句之后
            2.if语句可以有若干个else if 语句,但他们必须都在else语句之前
            3.else语句检测为true,其他所有语句都跳过执行
            4.else语句检测为false后,一旦有一个else if 语句检测为true,其他else if 和 else 语句都将跳过执行
            5.注意区间问题
             */
            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>=60){
                System.out.println("及格");
            }else if(score>=0 && score<60){
                System.out.println("不及格");
            }else{
                System.out.println("不合法数字");
            }
            scanner.close();
        }
    }
    

嵌套的if结构

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

  • 代码

    if(布尔表达式1){
        ///如果表达式1的值为true执行这里的代码
           if(布尔值表达式2){
               ///如果布尔值表达式2的值为true执行这里的代码
           }
    }
    
posted @ 2022-07-17 21:31  来自土木的一员  阅读(33)  评论(0)    收藏  举报