Java SE 学习记录03

开始学习Java

遵从同学的指导,从Java se开始学习

黑马的JavaSE零基础入门

day03-01 判断结构

package com;

public class Demo03 {
    public static void main(String[] args){
        // 单if语句
        int age = 16;
        if (age >= 18){
            System.out.println("进入网吧");
        }
        System.out.println("回家吃饭");

        // 标准的if-else语句
        int num = 13;
        if (num % 2 == 0){
            System.out.println("偶数");
        } else {
            System.out.println("奇数");
        }

        // x和y的关系满足如下:
        // 如果x >= 3,那么y = 2x +1;
        // 如果-1 < x < 3,那么y = 2x;
        // 如果x <= -1,那么y = 2x -1;
        int x = -2;
        int y;
        if (x >= 3){
            y = 2 * x + 1;
        } else if (-1 < x && x <3){
            y = 2 * x;
        } else {
            y = 2 * x - 1;
        }
        System.out.println("结果是:" + y);

        // 练习,根据成绩判断等级
        // 90 - 100 优秀
        // 80 - 89 好
        // 70 - 79 良
        // 60 - 69 及格
        // 60以下 不及格
        int score = 167;
        if (90 <= score && score <= 100 ){
            System.out.println("你的成绩是优秀");
        } else if (80 <= score && score < 90){
            System.out.println("你的成绩是好");
        } else if (70 <= score && score <80){
            System.out.println("你的成绩是良");
        } else if (60 <= score && score < 70){
            System.out.println("你的成绩是及格");
        } else if (score >= 0 && score <60){
            System.out.println("你的成绩是不及格");
        } else { // 单独处理边界之外的不合理情况
            System.out.println("数据错误");
        }

        // 题目:使用三元运算符和标准的if-else语句分别实现:取两个数字之间的最大值
        int a = 10;
        int b = 20;

        /*
        // 首先使用三元运算符来实现
        int max = a > b ? a : b;
        System.out.println("最大值是:" + max);

         */

        // 使用if语句
        int max;
        if (a > b){
            max = a;
        } else {
            max = b;
        }
        System.out.println("最大值是:" + max);
    }
}


day03-07 选择结构

package com;

public class Demo03 {
    public static void main(String[] args){
        /*
        switch语句使用的注意事项:
        1.多个case后面的数值不能重复
        2.switch后面小括号当中只能是:
            基本数据类型:byte/short/char/int
            引用数据类型:String/enum
        3.switch语句格式可以很灵活:前后顺序可以颠倒,而且break语句可以省略
         */
        int num = 16;
        switch (num) {
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期日");
                break;
            default:
                System.out.println("数据不合理");
                break;
        }

    }
}


day03-09 循环结构

package com;

public class Cycle {
    public static void main (String[] args){
        /*
        // for循环
        for(int i = 1; i <= 10; i++){
            System.out.println("我错了" + i);
        }
        System.out.println("程序停止");
        System.out.println("===================");

         */

        /*
        // while循环,标准格式和拓展格式
        int i = 1; // 初始化
        while (i <= 10){ // 判断语句
            System.out.println("我错了"); // 循环体
            i++; // 步进语句
        }

         */

        /*
        // do...while循环,标准格式和拓展格式
        int i = 1;
        do {
            System.out.println("起来吧,地上怪凉!" + i);
            i++;
        } while (i <= 10);

         */

        // 练习:用循环求出1-100之间的偶数和

        /*
        // for循环
        int sum = 0;
        for (int i = 1; i <=100; i++){
            if (i % 2 == 0){
                sum += i;
            }
        }
        System.out.println("结果是:" + sum);

         */

        /*
        // while循环
        int i = 1;
        int sum = 0;
        while (i <= 10){
            if (i % 2 == 0){
                sum += i;
            }
            i++;
        }
        System.out.println("结果是:" + sum);

         */

        // do...while循环
        int i = 1;
        int sum = 0;
        do {
            if (i % 2 == 0){
                sum += i;
            }
            i++;
        } while (i <= 10);
        System.out.println("结果是:" + sum);
    }
}

day03-15 循环结构_break语句

package com;

public class Cycle {
    public static void main (String[] args){
        /*
        break关键字的用法有常见的两种:
        1.可以用在switch语句中,一旦执行,整个switch语句立刻结束
        2.可以用在循环语句中,一旦执行,整个循环语句立刻结束,结束循环

        关于循环的选择,建议:
        凡是次数确定的场景多用for循环,否则多用while循环
         */

        for (int i = 1; i <= 10; i++) {
            // 如果希望从第四次开始,后续都不要了,就要打断循环
            if (i == 4) {
                break;
            }
            System.out.println("Hello" + i);
        }
    }
}

day03-16 循环结构_continue语句

package com;

public class Cycle {
    public static void main (String[] args){
        /*
        continue一旦执行,立刻跳过当前循环剩余内容,马上开始下一次循环
         */
                 
        for (int i = 1; i <= 10; i++){
            if (i == 4){
                continue; // 跳过当前循环,直接进行下一次循环
            }
            System.out.println(i + "层到了");
        }   
    }
}

day03-18 循环结构_嵌套循环

package com;

public class Cycle {
    public static void main (String[] args){
        // 嵌套循环
        for (int hour = 0; hour < 24; hour++){
            for (int minute = 0; minute <60; minute++){
                for (int second = 0; second <60; second++){
                    System.out.println(hour + "时" + minute + "分" + second + "秒");
                }
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值