java流程控制02-顺序结构 03-选择结构

顺序结构

sequential structure

java的基本结构

----> A -----> B ----->

他是任何一个算法都离不开的一种基本算法结构

选择结构

If 单选择结构

grammer:

if (bool expression){
  // the code will be executed if bool expression is true
}

example

package com.zepei.struct;
import java.util.Scanner;
public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("please input: ");
        String s = scanner.nextLine();
        // judge whether strings are equal
        if (s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();
    }
}

if 双选择结构

需要两个判断,if-else结构

grammer:

if (bool expression){
  // if the value of bool expression is true
}else{
  // if the value of bool expression is false
}
package com.zepei.struct;
import java.util.Scanner;
public class Demo03 {
    public static void main(String[] args) {
        // if score higher than and equal to 60 is pass
        // or is no pass
        Scanner scanner = new Scanner(System.in);
        System.out.println("please input a score");
        int score = scanner.nextInt();
        if (score >= 60){
            System.out.println("pass");
        }else {
            System.out.println("no pass");
        }
        scanner.close();
    }
}

if多选择结构

grammer

if (bool expression 1{
  // if bp 1 is true
}else if (bool expression 2){
  // if bp 2 is true
}else if (bool expression 3){
  // if bp 3 is true
}else {
  // if all bp above is not true
}
package com.zepei.struct;
import java.util.Scanner;
public class Demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("please input a score");
        int score = scanner.nextInt();
        if (score == 100){
            System.out.println("full marks");
        }else if (score < 100 && score >= 90) {
            System.out.println("A");
        }else if (score < 90 && score >= 60) {
            System.out.println("good");
        }else {
            System.out.println("no pass");
        }
        scanner.close();
    }
}

嵌套的if结构

grammer:

if (bool expression 1){
  // if bp1 is true
  if (bool expression 2){
    //if bp2 is true
  }
}

switch多选择结构

判断一个变量与一系列值中的某个值是否相等,每个值称为一个分支。

grammer:

switch(expression){
  case value:
    //sentence
    break; // optional
  case value:
    //sentence
    break; // optional
  default: // optional
    //sentence
}

变量类型可以为:byte short int char String

case 标签必须为字符串常量或字面量

break语句用于判断继续输出,如果值匹配,有break,则终止,无break则穿透继续输出

break尽量不要省略

package com.zepei.struct;
import com.sun.prism.shader.Solid_TextureYV12_AlphaTest_Loader;

import java.util.Scanner;
public class Demo06 {
    public static void main(String[] args) {
        //case
        char grade = 'B';
        switch (grade){
            case 'A':
                System.out.println("good");
                break;
            case 'B':
                System.out.println("not bad");
                //break;
            case 'C':
                System.out.println("bad");
            default:
                System.out.println("unknown");
        }
    }
}

反编译

java — class(字节码文件)— 反编译 (idea)

其实是通过hashcode进行比较的

你可能感兴趣的:(javase)