选择结构(整理)

if选择结构

if单选择结构

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")){
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();
    }
}

if双选择结构

public class IfDemo02 {
    public static void main(String[] args) {

        //考试分数大于等于60分就是及格,小于60就是不及格
        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多选择结构

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        /*
        * if语句最多有1个else语句,语句在所有的else if语句之后
        * if语句可以有若干个else if语句。他们必须在else语句之前
        * 一旦其中一个else if语句为true,其他的else if以及else语句都将跳过执行
        * */
        System.out.println("请输入成绩:");
        int score=scanner.nextInt();
        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();
    }
}

嵌套的if结构

switch多选择结构

switch多选择结构

  • 多选择结构还有一个实现方式就是switch case语句
  • switch case语句判断一个变量与一系列值中某个值是否想等,每个值称为一个分支。

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透  //switch  匹配一个具体的值
        char grade='B';
        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("未知等级");
        }
    }
}

case后基本要跟一个break,不然会输出其他输出:

grade为B时就会输出后面的输出,在case'D'后加一个break:

输出D后就结束了运行。

switch语句中的变量类型

  • byte、short、int或者char。(八大类型)
  • 从Java SE 7开始switch支持字符串String类型了
  • 同时case标签必须为字符串常量或字面量。
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name="张三";
        //JDk7的新特性,表达式结果可以是字符串!!!
        //字符的本质还是数字

        //反编译  java---class(字节码文件)----反编译(IDEA)

        switch(name){
            case"张三":
            System.out.println("张三");
            break;
            case"李四":
             System.out.println("李四");
            break;
            default:
                System.out.println("无");
        }
    }
}

int:直接数字:5

char:单引号且只能一个字符:‘5’

string:双引号随便长度:”65645“

While循环

计算1~100的和

public class WhileDemo02 {
    public static void main(String[] args) {
        int i=0;
        double sum=0.0;

        while(i<100){   //while(i<=100){
            i++;        //    sum=sum+i;
            sum=sum+i;  //    i++:
        }               //}
        System.out.println(sum);
    }
}

do...while循环

int i=0;
int sum=0;
do {
    sum=sum+i;  //    i++;
    i++;        //    sum=sum+i;
}while(i<=100); //}while(i<100)
System.out.println(sum);

//先运算就等,先增就不等

证明do...while会运行一次再判断布尔值

   int a=0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        //a=0,布尔值为false,因此不会走while,不会输出a
        System.out.println("========");
        do {
            System.out.println(a);
            a++;
        }while(a<0);
        System.out.println(a);
        //首先运行一次,输出a的值为0,之后再a++,a=1,此时布尔值为false,跳出do...while,再输出a的值1

输出结果

可以证明,while没有运算,do...while运算了一次


for循环

for与while的区别

public class ForDemo01 {
    public static void main(String[] args) {
    
        int a =1; //初始化条件
        while (a<=100){ //条件判断
            System.out.println(a);//循环体
            a+=2; //迭代 a=a+2
        }
        System.out.println("while循环结束!");

        //初始化值;条件判断;迭代
        for (int i=1;i<=100;i++){
            System.out.println(i);
        }

        System.out.println("for循环结束");
    }

}

//100.for快捷方式

print与println

print:输出但不换行

println:输出并换行

练习题

  • 计算0~100之间奇数和偶数的和
public class ForDemo02 {
    public static void main(String[] args) {

        //0~100奇数和偶数的和
        int oddSum=0;
        int evenSum=0;
        for (int i = 0; i <= 100; i++) {
            if (i%2==0){
                evenSum=evenSum+i;
            }else{
                oddSum=oddSum+i;
            }
        }
        System.out.println("奇数的和为:"+oddSum);
        System.out.println("偶数的和为:"+evenSum);
    }
}

  • 用while或for循环输出1~100之间能被5整除的数,并且每行输出3个

while方法

public static void main(String[] args) {
        int i = 0;
        int a = 0;
        while(i<=1000){
            if (i%5==0) {
                System.out.print(i + "\t");
                a++;
            }
            i++;
            if (a==3){
                System.out.println();
                a=0;
            }
        }
    }
}

for方法

public class ForDemo03 {
    public static void main(String[] args) {
        //用while或for循环输出1~100之间能被5整除的数,并且每行输出3个
        int a =0;
        for (int i = 0; i <=1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
                a++;
                if (a==3){
                    System.out.print("\n");
                    a=0;
                }
            }
        }
    }
}
  • 打印乘法口诀表

我自己打的

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

简化过后

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

没有用到sum变量,并且少了一个if语句

break continue

break

public class BreakDemo01 {
    public static void main(String[] args) {
        int i=0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;
            }
        }
        System.out.println("123");
    }
}

continue

public class ContinueDemo01 {
    public static void main(String[] args) {
        int i=0;
        while (i<100){
            i++;
            if (i%10==0) {
                System.out.println();
                continue;
            }
            System.out.print(i);
        }
        //break在任何循环语句的主体部分,均可用break控制循环的过程
        //break用于强行退出循环,不执行循环中剩余语句,(break也在switch语句中使用)
        //continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环中尚未执行的语句,按着下一次是否执行循环的判定
    }
}

反编译查看

第一步:点击右上角的Project Structure(Ctrl+Shift+Alt+S)

第二步:找到对应文件夹中的对应文件

第三步:将对应文件复制

第四部:打开IDEA中对应的代码,右键选择Open in Explorer

第五步:粘贴刚才复制的文件到这个文件夹下

第六步:在IDEA中会生成一个新的代码

第七步:由上图举例,点开SwitchDemo02,右键SwitchDemo02.class,选择矩形框中的任何一项进行查看

打印三角形

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

Debug

1.点击代码左行

2.点击Debug

3.如图点击,可以查看运行过程

posted @ 2021-04-12 22:42  HT625  阅读(77)  评论(0)    收藏  举报