后端学习记录~~JavaSE篇(Module03-流程控制语句-上)

if...else与Switch...case语句

一、表达式和语句

表达式:

(1)变量或常量 + 运算符构成的计算表达式

(2)new 表达式,结果是一个数组或类的对象。

(3)方法调用表达式,结果是方法返回值或void(无返回值)。

语句:

(1)分支语句:if...else,switch...case

(2)循环语句:for,while,do...while

(3)跳转语句:break,continue,return,throw

(4)try语句:try...catch...finally

(5)同步语句:synchronized

二、顺序结构

顺序结构就是程序从上到下逐行地执行。表达式语句都是顺序执行的。并且上一行对某个变量的修改对下一行会产生影响。

三、输入输出语句

换行:System.out.println(输出内容);//括号内可以什么都不写,用作换行处理

不换行:System.out.print(输出内容);//不写会报错

()中的内容使用 + 连接多项内容

格式化输出(了解)

 输入语句--代码示例:

import java.util.Scanner;

public class TestInput {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个整数:");//先执行,先显示这句话
        int num = input.nextInt();//接收键盘输入, 它们有顺序要求
        System.out.println("num = " + num);
        
        System.out.print("请输入一个小数:");
        double d = input.nextDouble();
        System.out.println("d = " + d);

        System.out.print("请输入一个布尔值:");
        boolean b = input.nextBoolean();
        System.out.println("b = " + b);

        System.out.print("请输入一个大整数");
        long big = input.nextLong();
        System.out.println("big = " + big);

        System.out.print("请输入一个字符串");
        String str = input.next();
        System.out.println("str = " + str);

        System.out.print("请输入单个字符:");
        char c = input.next().charAt(0);
      
        System.out.println("c = " + c);

        input.close();//建议大家记得它,代码没有错误,但是会造成JVM以外的操作系统相关内存没有得到是否
    }
}

四、分支语句

(一). if...else

if(条件表达式){
      语句体;

代码示例:

import java.util.Scanner;

public class IfCase5 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入要查询的年份:");
        int year = scan.nextInt();
        System.out.println("请输入要查询的月份:");
        int month = scan.nextInt();
        if (year >0){
                if (month < 13 && month > 0) {
                    if (month == 2)
                        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                            System.out.println(year + "年是闰年");
                            System.out.println(month + "月份有29天");
                        }else {
                            System.out.println(year + "年是平年");
                            System.out.println(month + "月份有28天");
                        }
                    else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                        System.out.println(month + "月份有31天");
                    else
                        System.out.println(month + "月份有三十天");
                } else {
                    System.out.println("输入有误请重启后重新输入");
                }
            }
        else {
            System.out.println("您输入的年份有误");
        }
        scan.close();
    }
}

(二)switch...case

switch 语句具有穿透性

避免穿透用break;

避免穿透性代码示例:
import java.util.Scanner;

public class SwitchCase1 {
    public static void main(String[] args) {
//        格式化星期几
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入您所需要匹配的值");
        int week = scan.nextInt();
        switch (week){
            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;
        }
        input.close();
    }
}
利用其穿透性代码示例:
import java.util.Scanner;

public class SwitchCase2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入月份:");
        int season = scan.nextInt();
        switch (season){
            case 3:
            case 4:
            case 5:
                System.out.println("春季");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("夏季");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("秋季");
                break;
            case 12:
            case 1:
            case 2:
                System.out.println("冬季");
                break;
            default:
                System.out.println("您输入的月份无效");
        }
        input.close();
    }
}

五、if语句与switch语句比较

  • if语句的条件是一个布尔类型值,if条件表达式为true则进入分支,可以用于范围的判断,也可以用于等值的判断,使用范围更广。

  • switch语句的条件是一个常量值(byte,short,int,char,枚举,String),只能判断某个变量或表达式的结果是否等于某个常量值,使用场景较狭窄。

  • 当条件是判断某个变量或表达式是否等于某个固定的常量值时,使用if和switch都可以,习惯上使用switch更多。当条件是区间范围的判断时,只能使用if语句。

  • 另外,使用switch可以利用穿透性,同时执行多个分支,而if...else没有穿透性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值