Java基础练习二

Java基础练习二


计算器:

编写一个简单的计算器程序,要求用户输入两个数和操作符(+、-、*、/),然后根据操作符进行相应的运算,并输出结果。如果输入的运算符不是有效的运算符,则输出"无效的运算符"。

import java.util.Scanner;

public class Demo_Calculator {
    public static void main(String[] args) {
        System.out.println("请输入你要计算的式子,中间以空格分割");
        Scanner input = new Scanner(System.in);
        double num1 = input.nextDouble();
        String str = input.next();
        double num2 = input.nextDouble();

        switch (str) {
            case "+" -> System.out.println(num1 + num2);
            case "-" -> System.out.println(num1 - num2);
            case "*" -> System.out.println(num1 * num2);
            case "/" -> {
                if (num2 != 0) {
                	System.out.println(num1 / num2);} else {
                    System.out.println("你个小辣鸡,哪有分母为0的");
                }
            }
            default -> System.out.println("无效运算符");
        }
    }
}

季节

编写一个程序,根据用户输入的月份(1 到 12),输出该月份所属的季节。假设春季是 3到 5 月,夏季是 6 到 8 月,秋季是 9 到 11 月,冬季是 12、1 和 2月。如果输入的月份超出了范围,输出"输入错误"。

import java.util.Scanner;

public class DemoSeason {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int month = input.nextInt();

        if(month >= 2 && month <= 5) {
            System.out.println("春季");
        } else if (month >= 6 && month <=8) {
            System.out.println("夏季");
        } else if (month >= 9 && month <=11) {
            System.out.println("夏季");
        } else {
            System.out.println("冬季");
        }
    }
}

星座

编写一个程序,根据用户输入的月份和日期,输出该日期所在的星座。以下是一个简单的星座日期范围参考:

import java.util.Scanner;

public class DemoConstellation {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个日期,之间用空格分开");
        int month = input.nextInt();
        int day = input.nextInt();

        if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
            System.out.println("我猜你是水瓶座");
        } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
            System.out.println("我猜你是双鱼座");
        } else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
            System.out.println("我猜你是白羊座");
        } else if ((month == 4 && day >= 20) || (month == 5 && day <= 18)) {
            System.out.println("我猜你是金牛座");
        } else if ((month == 5 && day >= 21) || (month == 6 && day <= 20)) {
            System.out.println("我猜你是双子座");
        } else if ((month == 6 && day >= 21) || (month == 7 && day <= 22)) {
            System.out.println("我猜你是巨蟹座");
        } else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
            System.out.println("我猜你是狮子座");
        } else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
            System.out.println("我猜你是处女座");
        } else if ((month == 9 && day >= 23) || (month == 10 && day <= 22)) {
            System.out.println("我猜你是天秤座");
        } else if ((month == 10 && day >= 23) || (month == 11 && day <= 21)) {
            System.out.println("我猜你是天蝎座");
        } else if ((month == 11 && day >= 22) || (month == 12 && day <= 21)) {
            System.out.println("我猜你是射手座");
        } else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {
            System.out.println("我猜你是魔蝎座");
        }
    }
}

奖金

编写一个程序,根据员工的工龄来计算年终奖金。奖金计算规则如下(工资和工龄输入):

  • 工龄小于等于5年,奖金为工资的5%
  • 工龄大于5年且小于等于10年,奖金为工资的10%
  • 工龄大于10年,奖金为工资的15%
import java.util.Scanner;

public class DemoBonus {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int workAge = input.nextInt();
        double bonus = input.nextDouble();
        if (workAge <= 5) {
            System.out.println("奖金为" + bonus * 0.05);
        } else if (workAge <= 10) {
            System.out.println("奖金为" + bonus * 0.1);
        } else {
            System.out.println("奖金为" + bonus * 0.15);
        }
    }
}

if 分支和 switch 分支流程图

  • if 分支
    在这里插入图片描述
  • switch 分支

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值