Java流程控制(Scanner)

3.1用户交互Scanner

可以通过Scanner类来获取用户的输入:

  语法: Scanner s = new Scanner(System.in);

  通过Scanner类的next()nextLine()方法获取输入的字符串;  

  读取之前,使用hasNext()hasNextLine()来判断是否还有输入的数据。

 

 package com.hch.scanner;
 ​
 import java.util.Scanner;
 ​
 public class Demo01_Scanner {
     public static void main(String[] args) {
         //创建一个扫描器接收对象(键盘输入值)
         Scanner scanner = new Scanner(System.in);
         System.out.println("用next方法接收:");
         //判断用户有没有输入字符串
         if (scanner.hasNext()){
             String str = scanner.next();
             System.out.println("输出的内容:"+str);
        }
         //凡是属于IO流的类如果不关闭,则会一直占用资源。
         scanner.close();
         
         System.out.println("用nextLine方法接收:");
 //       if (scanner.hasNextLine()){
 //           String str1 = scanner.nextLine();
 //           System.out.println("输出内容2:"+str1);
 //       }
 //       scanner.close();
    }
 }
 ​

Scanner对象:

·next():

  1、一定要读取到有效字符后才可以结束输入。

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

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

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

·nextLine():

  1、以Enter键为结束符,即:输出所有Enter键之前的字符串包括空格

  2、可以获得空白

3.2顺序结构

  Java最基本的结构就是顺序结构。(从上往下顺序依次执行)

3.3选择结构

3.3.1 if单选择结构

if(布尔表达式){

//如果布尔表达式为true将要执行的语句

}

 package com.hch.struct;
 ​
 import java.util.Scanner;
 ​
 public class Demo01_if {
     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();
    }
 }
 ​

 

3.3.2if双选择结构

if(布尔表达式){

//如果布尔表达式为true的时候执行的语句

}else{

//如果为false执行的语句

}

 package com.hch.struct;
 ​
 import java.util.Scanner;
 ​
 public class Demo02_if {
     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();
    }
 }
 ​

 

3.3.3if多选择结构(注意:一个if语句可以有多个else if语句但是只能有一个 if 语句和一个 else 语句)

if(布尔表达式1){

//如果布尔表达式1为true时执行语句

}else if(布尔表达式2){

//如果布尔表达式2为true时执行

}...else if(布尔表达式n){

//如果布尔表达式n正确执行代码

}else{

//如果以上都不正确执行语句

}

 package com.hch.struct;
 ​
 import java.util.Scanner;
 ​
 public class Demo03_if {
     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){
             System.out.println("恭喜满分!!!");
        }else if (score < 100 && score >= 90){
             System.out.println("A");
        }else if (score < 90 && score >= 80){
             System.out.println("B");
        }else if (score < 80 && score >= 70){
             System.out.println("C");
        }else if (score < 70 && score >= 60){
             System.out.println("D");
        }else if(score <60 && score >=0){
             System.out.println("不及格");
        }else {
             System.out.println("非法成绩");
        }
         scanner.close();
    }
 }
 ​

 

3.3.4嵌套的if结构

if(布尔表达式1){

//如果布尔表达式1为true执行的代码

if(布尔表达式2){

//如果表达式2为true执行的语句.....

}

}

3.3.5switch多选择结构(switch case语句)

switch中的变量类型可以是:

  1、byte、short、int、或者char

  2、从JavaSE7开始,switch支持字符串String类型

  3、同时case标签必须为字符串常量或字面量

switch(expression){

case value:

//语句

break;//可选

case value:

//语句

break;//可选;case数量任意

default://可选

//语句

}

==关于break 和 continue的联系与区别:==

  1、break在任何循环语句的主体部分;continue用在循环语句体中。

  2、break用于强行退出循环,不执行剩下的语句;continue用于跳过尚未执行的语句,接着下一次是否执行循环的判断

 

 package com.hch.struct;
 ​
 public class Demo03_switch {
     public static void main(String[] args) {
         
         char grade = 'A';
         //switch ==> 匹配一个值
         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("未知等级");
        }
    }
 }
 ​

3.4循环结构

3.4.1 while循环

  1、只要布尔值为true,循环就会一直执行下去

  2、避免死循环(我们需要一个表达式让循环停止或者失效)

while(布尔表达式){

//循环内容

}

 package com.hch.struct;
 ​
 public class Demo05_while {
     public static void main(String[] args) {
 //       int i = 0;
 //       while (i<=100){
 //           i++;
 //       }
 //       System.out.println(i);
 ​
         int i = 0;
         int sum = 0;
         while (i<=100){
             sum = sum + i;
             i++;
        }
         System.out.println(sum);
    }
 }

 

3.4.2 do...while循环

  1、与while不同。do while语句至少执行一次,do后面的语句。

  2、while先判断后执行,do while先执行后判断。

do{

//语句

}while(布尔表达式);

 package com.hch.struct;
 ​
 public class Demo06_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);
    }
 }

 

3.4.3 for循环

  1、支持迭代的一种通用结构,是最有效、最灵活的循环结构。

  2、最先执行初始化步骤,可以声明一个或多个控制变量也可为空值。

  3、然后检测布尔值是否为true,是则执行,否则跳出。

for(初始化;布尔表达式;更新操作){

//语句

}

 package com.hch.struct;
 ​
 public class Demo07_for {
     public static void main(String[] args) {
 //       for (int i =0;i<=100;i++){
 //           System.out.println(i);
 //       }
 //       100.for 快捷生成IDEA生成
 //       int oddSum = 0;
 //       int evenSum = 0;
 //       for (int i = 0;i <= 100; i++){
 //           if (i%2 != 0){ //i/2余数不等于0 为奇数
 //               oddSum+=i;
 //           }else { //偶数
 //               evenSum+=i;
 //           }
 //       }
 //       System.out.println();
 //       System.out.println("奇数和:"+oddSum);
 //       System.out.println("偶数和:"+evenSum);
         for (int i = 1;i <= 9;i++){
             for (int j = 1;j <= i; j++){
                 System.out.print(i+"*"+j+"="+(j*i) + " ");
            }
             System.out.println();
        }
    }
 }
原文地址:https://www.cnblogs.com/joey-413/p/13234794.html