JAVA作业一:简单的几个小程序

6 篇文章 0 订阅
这篇博客展示了如何使用Java编程实现不同功能,包括计算特定月份的天数、出租车费用、阶乘和完全数,以及解决鸡蛋数量问题。程序通过用户选择功能并输入参数,利用switch-case结构调用相应函数执行计算。示例代码中包含详细的注释,解释了每个功能的实现逻辑。
摘要由CSDN通过智能技术生成

1,输入年份和月份,输出该年中该月的天数
2,出租车计费问题:起步2公里内5元,3公里内以上每公里收费1.3元,9公里以内以上每公里收费2元,燃油附加费1元,编写程序,输入公里数,计算所需的出租车费用
3,分别用do-while和for循环计算1+1/2!+1/3!+…的前20项之和并输出
4,求1000以内的完全数(一个数等于它的因子之和称为完全数)
5,有一个问题,一筐鸡蛋:
1个1个拿,正好拿完
2个2个拿,还剩1个
3个3个拿,正好拿完
4个4个拿,还剩1个
5个5个拿,还差1个
6个6个拿,还剩3个
7个7个拿,正好拿完
8个8个拿,还剩1个
9个9个拿,正好拿完
我是把五个小作业都做成功能函数,然后在主函数中利用switch–case语句调用功能函数来完成的。
实验环境:Java-IDEA
运行效果截图:
在这里插入图片描述

贴出全部代码:

package 实验.作业1;

import java.util.Scanner;

public class y_06_综合 {
    public static void main(String[] args) {
        int select;
        Scanner sc = new Scanner(System.in);
        System.out.println("1,月的天数");
        System.out.println("2,出租车计费问题");
        System.out.println("3,求和问题");
        System.out.println("4,1000以内的完全数");
        System.out.println("5,求最小个数");
        System.out.println("0,退出系统");

        System.out.printf("请选择:");
        select = sc.nextInt();
        if(select < 0 || select > 7){
            System.out.println("选择有误。");
        }
        else{
            switch (select){
                case 1:
                    System.out.printf("请输入年份和月份:");
                    int year = sc.nextInt();
                    int month = sc.nextInt();
                    ts(year,month);
                    break;

                case 2:
                    System.out.printf("请输入总公里数:");
                    float a = sc.nextFloat();  //定义一个浮点型变量记录打车总公里数
                    float price = 0;
                    jf(a,price);
                    break;

                case 3:
                    float b = 0;
//                    int c = 1;
                    for(int c = 1;c <= 20;c++){
                        b = b + (float)1/jc(c);
//                    b = b + 1/jc(c)不会报错但是结果出错,
//                     因为b为float型,1/jc(c),整型除以整型结果还是整型
//                    do{
//                        b = b + (float)1/jc(c);
//                        c++;
//                    }
//                    while(c <= 20);
                    }
                    System.out.printf("前二十项之和是:%f",b);
                    break;

                case 4:
                    for(int d = 1;d <= 1000;d++)
                        if(d == yz(d)){
                            System.out.printf("%d 是完全数\n",d);
                        }
                    break;

                case 5:
                    int count = 0;
                    for(int i = 1;i <= 10000;i++){
                        if(i%2 == 1 && i%4 == 1 && i%5 == 1 && i%8 == 1){
                            if(i%3 == 0 && i%7 == 0 && i%9 == 0){
                                if(i%6 == 3){
                                    System.out.printf("框里最少有 %d 个鸡蛋\n",i);
                                    count++;  //利用count计数,只输出第一个符合条件的数
                                    if(count>=1)
                                        break;
                                }
                            }
                        }
                    }
                    break;
                case 0:
                    System.exit(-1);
                    break;
            }

        }
    }
    public static void ts(int year,int month){
        switch (month){
            default:
                System.out.println("输入年份或月份的格式有误。");
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                System.out.printf("%d年%d月有31天。\n",year, month);
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                System.out.printf("%d年%d月有30天。\n",year, month);
                break;
            case 2:
                //判断是否是闰年
                if((year%4 == 0&&year%100 != 0)||year%400 == 0){
                    System.out.printf("%d年%d月有29天。\n",year, month);
                }
                else
                    System.out.printf("%d年%d月有28天。\n",year, month);
                break;
        }
    }  //函数:判断月的天数
    public static void jf(float a,float price){
        if (a <= 2)
            price = 5;
        else if (a>2 && a<=9)
            price = 5 + (a - 2) * 1.3f;
        else
            price = 5 + 7 * 1.3f + (a - 9) * 2;
        price += 1;
        System.out.println("所需的出租车费用为" + price);
    }  //函数:出租车计费
    static int jc(int num){
        //利用递归求阶乘
        int sum = 1;
        if(num <= 0){
            System.out.println("所给数字格式有误。");
        }
        if(num == 1){
            return 1;
        }
        else{
            sum = num * jc(num-1);
            return sum;
        }
    }  //函数:计算阶乘
    public static  int yz(int a){
        int sum = 0;
        for(int i = 1;i < a;i++){
            if(a%i == 0)
                sum += i;
        }
        return sum;
    }  //函数:计算一个数的因子之和
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值