java流程控制

1. 1、用户交互Scanner

1.1. 作用

Scanner类用于获取用户的输入以达到程序与人之间的交互

1.2. 基本语法

Scanner s = new Scanner(System.in); 

通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串,在读取前我们一般需要 使用 hasNext() 与 hasNextLine() 判断是否还有输入的数据

1.3. 列子

package Scanner;
import java.util.Scanner;
public class Demo01 {
    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();

    }
}

1.4. Next与NextLine

next():

  • 1、一定要读取到有效字符后才可以结束输入。
  • 2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
  • 3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
  • 4、next() 不能得到带有空格的字符串

nextLine():

  • 1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
  • 2、可以获得空白

2. 2、流程结构

2.1. 顺序结构

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

顺序结构是最简单的算法结构。

f019b4281a8c49f446982a80fbf95559_MD5.png

2.2. 选择结构

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

d863049e977e2c88891760f5419d3ba2_MD5.png

2.2.1. if 单选结构

import java.util.*;
public class ifDemo01 {
    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("输如不相等");
        scanner.close();
    }
}

2.2.2. if 双选结构

if else

import java.util.*;
public class ifelse {
    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();
    }
}

2.2.3. if多选择结构

else if

import java.util.*;
public class manyif {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int score= scanner.nextInt();
        if(score>=90){
            System.out.println("A");
        }else if(score>=80 && score<90){
            System.out.println("B");
        }else if(score>=70 && score<80){
            System.out.println("C");
        }else if(score>=60 && score<70){
            System.out.println("D");
        }else{
            System.out.println("E");
        }
        scanner.close();
    }
}

2.2.4. 嵌套的if结构

if嵌套if

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

2.2.5. switch多选择结构

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

switch case 语句有如下规则:

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。 case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现break 语句。
  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

case穿透现象

import java.util.*;
public class switchDemo01 {
    public static void main(String[] args) {
        char grade = 'B';

        switch (grade) {
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
            case 'C':
                System.out.println("及格");
            default:
                System.out.println("未知等级");
        }
    }
}

只有A后面有break grade=A时,就只会输出优秀,然后就break,但是输入B就会输出 良好 及格 未知等级,而输入C就只会输出 及格、未知等级

每一个对象都有一个hashcode

3. 3、循环节结构

3.1. while循环

public class whileDemo01 {
    public static void main(String[] args) {
        int i = 1;
        while(i<=100){
            i+=i;
            System.out.println(i);
        }
    }
}

3.2. do while 循环

public class Dowhile {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do{
            sum+=i;
            i++;
            System.out.println(sum);
        }while(i<=100);
    }
}

3.3. for循环

public class forDemo01 {
    public static void main(String[] args) {
        for(int i=0;i<=100;i++){
            System.out.println(i);
        }
    }
}

99乘法表

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

    }
}

3.4. 增强的for循环

public class beterFor {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4};
        for(int i:numbers) {
            System.out.println(i);
        }
    }
}

4. 练习

4.1. 打印三角形

public class triangle {
    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 k=1;k<=i;k++){
                System.out.print("*");
            }
            for(int k=1;k<i;k++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}