7、流程控制

7、流程控制

7.1、Scanner

通过Scanner来获得用户输入

next():

  1. 一定要督导有效字符后才可以结束输入。

  2. 对输入有效字符之前遇到的空白啊啊,next()方法会自动将其去掉

  3. 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符

  4. next()不能得到带有空格的字符串

  5. package scanner;
    
    import java.util.Scanner;
    
    public class Demo1 {
        public static void main(String[] args) {
    
    //        创建一个扫描对象,用于接收键盘数据
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("使用next方法接收:");
            // 判断用户有没有输入字符串
            if (scanner.hasNext()) {
                // 使用next方法接收
                String str = scanner.next();
                System.out.println("输入的内容为" + str);
            }
    
            scanner.close();
        }
    }
    
    

nextLine()

  1. 以Enter为结束符,

  2. 可以获得空白

  3. package scanner;
    
    import java.util.Scanner;
    
    public class Demo2 {
        public static void main(String[] args) {
    
            Scanner scanner = new Scanner(System.in);
    
            System.out.print("使用nextLine方式接收:");
            if (scanner.hasNext()) {
                String str = scanner.nextLine();
                System.out.println("输出内容为" + str);
            }
            scanner.close();
        }
    }
    
    

7.2、顺序结构

语句从上往下依次执行,是一个基本结构

7.3、选择结构

  • if单选则结构

    • 语法

    • if(布尔表达式){
          //如果布尔表达式为true将执行的语句 
      }
      
    • public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入内容");
            String s = scanner.nextLine();
        
            if (s.equals("hello")) {
                System.out.println(s);
            }
            System.out.println("End");
      
          scanner.close();
      }
      
    • 
      
  • if双选择结构

    • 语法

    • if(布尔表达式){
          //如果布尔表达式为true将执行的语句 
      }else{
          //如果布尔表达式为flase将执行的语句 
      }
      
    •     public static void main(String[] args) {
              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();
          }
      
  • if多选择结构

    • 语法

    • if(布尔表达式 1){
          //如果布尔表达式1为true将执行的语句 
      }else if (布尔表达式 2){
          //如果布尔表达式2为true将执行的语句 
      }else if (布尔表达式 3){
          //如果布尔表达式3为true将执行的语句 
      }else{
          //如果以上都布尔表达式都不为true执行语句
      }
      
    • public static void main(String[] args) {
             Scanner scanner = new Scanner(System.in);
             System.out.println("请输入成绩:");
        
             int score = scanner.nextInt();
             if (score <= 100) {
                 if (score < 100 && score >= 90) {
                     System.out.println("A");
                 } else if (score < 90 && score >= 70) {
                     System.out.println("B");
                 } else if (score < 70 && score >= 60) {
                     System.out.println("C");
                 } else {
                     System.out.println("成绩不及格");
                 }
             } else {
                 System.out.println("成绩不合法");
             }
        
             scanner.close();
         }
      
  • 嵌套的if结构

    • public static void main(String[] args) {
             Scanner scanner = new Scanner(System.in);
             System.out.println("请输入成绩:");
        
             int score = scanner.nextInt();
             if (score <= 100) {
                 if (score < 100 && score >= 90) {
                     System.out.println("A");
                 } else if (score < 90 && score >= 70) {
                     System.out.println("B");
                 } else if (score < 70 && score >= 60) {
                     System.out.println("C");
                 } else {
                     System.out.println("成绩不及格");
                 }
             } else {
                 System.out.println("成绩不合法");
             }
        
             scanner.close();
         }
      
  • switch多选择结构

    • 语法

    • switch (expression){
           case value :
              //语句
              break;//可选
            case value :
              //语句
              break;//可选
            case value :
              //语句
              break;//可选
            default : //可选
              //语句
                      
      }
      
    • public static void main(String[] args) {
        
             Scanner scanner = new Scanner(System.in);
        
             System.out.println("请输入成绩:");
             String a = scanner.nextLine();
             switch (a) {
                 case "90":
                     System.out.println("优秀");
                     break;
                 case "80":
                     System.out.println("良好");
                     break;
                 case "70":
                     System.out.println("还行");
                     break;
                 case "60":
                     System.out.println("及格");
                     break;
                 default:
                     System.out.println("不及格");
             }
             
             scanner.close();
         }
      
    • 注意case穿透

7.4、循环结构

  • while循环

    • 语法

    • while(布尔表达式){
          //循环内容
      }
      
    • public static void main(String[] args) {
      
              int i = 0;
      
              while (i < 100) {
                  i++;
                  System.out.println(i);
              }
          }
      
    • 只要为true就会一直执行下去

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

  • do...while循环

    • 语法

    • do {
          // 代码
      }while(条件)
          
      
    •     public static void main(String[] args) {
              
      
              int i = 0;
              int sum = 0;
      
              do {
                  sum = sum + i;
                  i++;
              } while (i <= 100);
              System.out.println(sum);
          }
      
    • 至少执行一次代码

  • For循环

    • 语法

    • for (初始化值; 条件; 迭代) {
                 //语句;
             }
      
    • public static void main(String[] args) {
        
             int sum = 0;
             for (int i = 0; i <= 100; i++) {
                 sum = sum + i;
             }
             System.out.println(sum);
         }
      
    • for循环是最有效,最灵活的循环结构

    • for (;;){
                 
             }//死循环
      
    • 计算0到100奇偶之和

    • public static void main(String[] args) {
             int oddSum = 0;
             int evenSum = 0;
        
             for (int i = 0; i < 100; i++) {
                 if (i % 2 != 0) {
                     oddSum += i;
                 } else {
                     evenSum += i;
                 }
             }
             System.out.println(oddSum);
             System.out.println(evenSum);
             System.out.println(oddSum + evenSum);
      
      }
      
    • 打印九九乘法表

    •     public static void main(String[] args) {
              for (int i = 1; i <= 9; i++) {
                  for (int j = 1; j <= i; j++) {
                      System.out.print(j + "*" + i + "=" + (j * i) + "\t");
                  }
                  System.out.println();
              }
      
          }
      

break:直接结束循环体

contine:结束本次循环

练习 打印三角形

   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();
        }
    }
posted @ 2021-02-27 23:00  大格子  阅读(48)  评论(0)    收藏  举报