010if_switch语句

程序流程控制

顺序结构

顺序结构是指程序按照语句的书写顺序依次执行,没有中间的判断、跳转或者循环,每一条语句都是顺序执行,没有跳过或重复的部分

选择结构

选择结构根据条件判断的结果选择执行不同的代码,选择结构可以细分为单分支结构,双分支结构和多分支结构,在Java中,可以使用if语句和switch语句实现选择结构

循环结构

循环结构根据判断条件重复执行某段代码,Java语言提供了while循环,dou-while循环和for循环来实现循环结构,此外JDK1.5引入了for-each循环,用于迭代(遍历)数据和集合

switch结构

switch(表达式)

表达式支持的类型:

byte\int\char\String\short\枚举型

相关代码练习

package com.hailong.branch;
import java.util.Scanner;
/**
 * author:hlc
 * date:2023/8/2
 */
public class Demo1 {
    /**
     * 编写一个程序,根据输入的年份和月份来输出该月份的天数。要考虑闰年的情况。
     * 判断闰年:能够被400整除。
     * 或者能够被4整除但是不能被100整除
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = sc.nextInt();
        System.out.println("请输入月份");
        int month = sc.nextInt();
        switch (month){
            case 1,3,5,7,8,10,12 -> System.out.println(year + "年的" + month + "月是31");
            case 4,6,9,11 -> System.out.println(year + "年的" + month + "月是30");
            case 2 ->{
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){//是闰年
                    System.out.println(year + "年的" + month + "月是29");
                }else {
                    System.out.println(year + "年的" + month + "月是28");
                }
            }
        }
    }
}

package com.hailong.branch;

import java.util.Scanner;

/**
 * author:hlc
 * date:2023/8/2
 */

/**
 * 编写一个程序,根据用户输入的温度单位("C"表示摄氏度,"F"表示华氏度)和温度,将其
 * 转换为另一种单位后输出。
 * 注: 摄氏度转换为华氏度的公式(摄氏度 * 9 / 5)+32,以及华氏度转换为摄氏度的公式(华
 * 氏度-32) * 5 / 9
 */
public class Demo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入温度");
        double temperature = sc.nextDouble();
        System.out.println("请输入你输入温度的单位 (c  /  f)");
        String unit = sc.next();
        /*
        注: 摄氏度转换为华氏度的公式(摄氏度 * 9 / 5)+32,以及华氏度转换为摄氏度的公式(华
          氏度-32) * 5 / 9
         */
        switch (unit) {
            case "c", "C" -> {
                System.out.println("转换后的华氏是 :" + ((temperature * 9 / 5) + 32) +"华氏度");
            }
            case "f", "F" -> {
                System.out.println("转换后的摄氏度是 :" + ((temperature - 32) * 5 / 9)+"摄氏度");
            }
        }
    }
}

package com.hailong.branch;

import java.util.Scanner;

/**
 * author:hlc
 * date:2023/8/2
 */
public class Demo3 {
    public static void main(String[] args) {
        /**
         * 编写一个简单的计算器程序,要求用户输入两个数和操作符(+、-、*、/),然后根据操
         * 作符进行相应的运算,并输出结果。如果输入的运算符不是有效的运算符,则输出"无效的
         * 运算符
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入两个数");
        double x = sc.nextDouble();
        double y = sc.nextDouble();
        System.out.println("请输入你要执行什么操作 (+、-、*、/)");
        String str = sc.next();
        switch (str){
            case "+" -> System.out.println(String.format("%.0f",x+y));
            case "-" -> System.out.println(String.format("%.0f",x-y));
            case "*" -> System.out.println(String.format("%.0f",x*y));
            case "/" -> System.out.println(String.format("%.2f",x/y));
        }
    }
}

package com.hailong.branch;

import java.util.Scanner;

/**
 * author:hlc
 * date:2023/8/2
 */
public class Demo4 {
    public static void main(String[] args) {
        /**
         * 编写一个程序,根据用户输入的月份(1 到 12),输出该月份所属的季节。假设春季是 3
         * 到 5 月,夏季是 6 到 8 月,秋季是 9 到 11 月,冬季是 12、1 和 2月。如果输入的月份超
         * 出了范围,输出"输入错误
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入月份");
        int month = sc.nextInt();
        switch (month){
            case 3,4,5 -> System.out.println("这会是春季");
            case 6,7,8 -> System.out.println("这会是夏季");
            case 9,10,11 -> System.out.println("这会是秋季");
            case 12,1,2 -> System.out.println("这会是冬季");
        }
    }
}

package com.hailong.branch;

import java.util.Scanner;

/**
 * author:hlc
 * date:2023/8/2
 */
public class Demo5 {
    public static void main(String[] args) {
        /**
         * 编写一个程序,根据用户输入的月份和日期,输出该日期所在的星座。以下是一个简单的
         * 星座日期范围参考:
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的月份");
        int month = sc.nextInt();
        System.out.println("请输入你的日期");
        int day = sc.nextInt();
        switch (month) {
            case 1 -> {
             if (day > 20){
                 System.out.println("水瓶座");
             }else {
                 System.out.println("魔羯座");
             }
            }
            case 2 -> {
                if (day < 18){
                    System.out.println("水平座");
                }else {
                    System.out.println("双鱼座");
                }
            }

            case 3 -> {
                if (day < 20){
                    System.out.println("双鱼座");
                }else {
                    System.out.println("白羊座");
                }
            }

            case 4 -> {
                if (day < 19){
                    System.out.println("白羊座");
                }else {
                    System.out.println("金牛座");
                }
            }
            case 5 -> {
                if (day < 20){
                    System.out.println("金牛座");
                }else {
                    System.out.println("双子座");
                }
            }
            case 6 -> {
                if (day < 20){
                    System.out.println("双子座");
                }else {
                    System.out.println("巨蟹座");
                }
            }
            case 7 -> {
                if (day < 22){
                    System.out.println("巨蟹座");
                }else {
                    System.out.println("狮子座");
                }
            }
            case 8 -> {
                if (day < 22){
                    System.out.println("狮子座");
                }else {
                    System.out.println("处女座");
                }
            }

            case 9 -> {
                if (day < 22){
                    System.out.println("处女座");
                }else {
                    System.out.println("天秤座");
                }
            }

            case 10 -> {
                if (day < 22){
                    System.out.println("天秤座");
                }else {
                    System.out.println("天蝎座");
                }
            }
            case 11 -> {
                if (day < 21){
                    System.out.println("天蝎座");
                }else {
                    System.out.println("射手座");
                }
            }
            case 12 -> {
                if (day < 21){
                    System.out.println("射手座");
                }else {
                    System.out.println("魔羯座");
                }
            }
        }
    }
}

package com.hailong.branch;

import java.util.Scanner;

/**
 * author:hlc
 * date:2023/8/2
 */
public class Demo6 {
    public static void main(String[] args) {
        /**
         * 编写一个程序,根据员工的工龄来计算年终奖金。奖金计算规则如下:
         * 工龄小于等于5年,奖金为工资的5%
         * 工龄大于5年且小于等于10年,奖金为工资的10%
         * 工龄大于10年,奖金为工资的15%
         * (工资和工龄输入)
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入员工的工资");
        double salary = sc.nextDouble();
        System.out.println("请输入员工的工龄");
        int year = sc.nextInt();
        if (year <= 5) {
            System.out.println("奖金为" + salary * 0.05);
        } else if (year > 5 && year <= 10){
            System.out.println("奖金为" + salary * 0.1);
        }else if (year>10){
            System.out.println("奖金为" + salary * 0.15);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值