Day10

2022.12.13

switch多选择结构

package struct;

public class Demo_switch {
    public static void main(String[] args) {
        //switch   匹配一个具体的值
        //case穿透(无break时)

        char grade ='C';

        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("未知等级");
        }

    }
}

package struct;

public class Demo02_switch {
    public static void main(String[] args) {
        //JDK7的新特性,表达式结果可以是字符串
        //字符的本质还是数字

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

        String name = "杜兰特";

        switch (name){
            case "詹姆斯":
                System.out.println("詹姆斯");
                break;
            case "杜兰特":
                System.out.println("杜兰特");
                break;
            default:
                System.out.println("其他");
        }
    }
}

switch语句

在java中控制流程语句是由选择语句、循环语句、跳转语句构成。选择语句包括 if 和 switch,在过多的使用 if 语句嵌套会使程序很难阅读,这时利用 switch 语句可以有效的处理多重条件并是程序的可读性增加。

switch语句遵从规则

  • 如果 case 语句块中没有 break 语句时,匹配成功后,从当前 case 开始,后续所有 case 的值都会输出。
  • 如果当前匹配成功的 case 语句块没有 break 语句,则从当前 case 开始,后续所有 case 的值都会输出,如果后续的 case 语句块有 break 语句则会跳出判断。
  • 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 语句。

每日英语学习

单词

  • embrace
  • escape
  • examine
  • feedback
  • amateur
  • administration
  • array
  • assess
  • assume
  • belief
  • cover
  • craftsmanship
  • discover
  • electronic

金句

  • The way to get started is quit talking and begin doing.

  • People are lonely because they build walls and instead of bridges.(人们之所以孤,是因为他们筑墙而不是桥梁)

  • Knowledge is the eye of desire and can become the pilot of the soul.(知识是欲望之眼,可以成为领会的领航员)

2022.12.14

循环结构

  • while循环
  • do…while循环
  • for循环

while循环

package struct;

public class Demo_while {
    public static void main(String[] args) {
        //计算1+...+100
        //高斯求和

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

DoWhile循环

//程序一
package struct;

public class Demo_dowhile {
    public static void main(String[] args) {
        //计算1+...+100

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






//程序二
package struct;

public class Demo2_dowhile {
    public static void main(String[] args) {
        int a = 0;
        while (a<0){
            System.out.println(a);
        }

        System.out.println("==================");

        do {
            System.out.println(a);
        }while (a<0);
    }
}

For循环

关于for循环有以下几点说明

  • 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
  • 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
  • 执行一次循环后,更新循环控制变量。
  • 再次检测布尔表达式。循环执行上面的过程。
//程序一
package struct;

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

        int a = 1;//初始化条件

        while (a<=100){  //条件判断
            System.out.println(a);//循环体
            a+=2;//迭代
        }

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

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

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

    //快捷写法
    //100.for(用于IDEA)
}






//程序二
package struct;

public class Demo02_For {
    public static void main(String[] args) {
        //练习1:计算1到100之间的奇数和偶数的和

        int oddSum = 0;//奇数之和
        int evenSum = 0;//偶数之和

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





//程序三
package struct;

public class Demo03_For {
    public static void main(String[] args) {
        //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
        for (int i = 1; i<=1000; i++){
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println();
                //System.out.print("\n");
            }
        }


        System.out.println();
        System.out.println();
        System.out.println("========================");
        System.out.println();

        int a = 1;
        while (a<=1000){
            if (a%5==0){
                System.out.print(a+"\t");
            }
            if (a%(5*3)==0){
                System.out.println();
            }
            a++;
        }

        //println  输出完会换行
        //print  输出完不会换行
    }
}






//程序四:九九乘法表
1*1=1	
2*1=2	2*2=4	
3*1=3	3*2=6	3*3=9	
4*1=4	4*2=8	4*3=12	4*4=16	
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81
    
package struct;

public class Demo04_For {
    public static void main(String[] args) {
        /*
        1,先打印第一列
        2,在打印九行九列
        3,去掉重复项
        4,调整样式
         */

        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i+"*"+j+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
}
    

每日英语学习

单词

  • deduct

  • subtract

  • reduce

  • decline

  • decrease

  • subject

  • object v.反对

  • project

  • inject

  • recycle

  • dispose

  • consume

  • garbage

  • litter

  • various

  • ambiguous

  • conscientious

  • indigenous

  • competent

  • complement

  • compliment

  • occupation occupy

  • vocation vacation

  • personnel

  • dentist

  • apprentice

  • author

  • knight

  • animate

  • activate

  • label
    tract**

  • reduce

  • decline

  • decrease

  • subject

  • object v.反对

  • project

  • inject

  • recycle

  • dispose

  • consume

  • garbage

  • litter

  • various

  • ambiguous

  • conscientious

  • indigenous

  • competent

  • complement

  • compliment

  • occupation occupy

  • vocation vacation

  • personnel

  • dentist

  • apprentice

  • author

  • knight

  • animate

  • activate

  • label

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值