2021-05-29

本文探讨了Java中的方法重载规则,包括返回值不设限、方法名相同但形参列表各异,并通过实例展示了如何实现返回值为double的方法重载。同时,深入讲解了方法递归的概念,以求阶乘、和求和和打印斐波那契数列为例,强调了递归在解决问题中的应用及与循环的区别。
摘要由CSDN通过智能技术生成

一、方法重载:
方法重载规则
1.方法返回值不做要求
2.方法名必须相同
3.形参列表不同,不同指的是形参类型不同或者形参个数不同
4.方法名和形参列表必须同时满足要求

在这里插入代码片
/**方法重载
*/
public class Method{
	public static void main(String[] args){
		int x = 1;
		int y = 6;
		int z = 57;
		System.out.println(method(x,y));
		System.out.println(method(x,y,z));
	/**
	如果要调用返回值为double的这个方法,则需要给double类型的形参,比如 System.out.println(method(2.1,3.2,4.2)); 这样就可以调用*/
		
	}
	public static int method(int a,int b){
	return a+b;
	}
	public static int method(int a,int b,int c){
	return a+b+c;
	}
	public static double method(double a,double b,double c){
	return a+b+c;
	}
}

总结:重载规则针对于在同一个类中的方法

二、方法递归:
1.要调用自身
2.有一个趋近于终止的条件
3.推导出递推公式

1.求N的阶乘的例子
//求N的阶乘
public static int fac(int n) {
    if(n == 1){
        return 1;
    }
    int count = n*fac(n-1);
    return count;
}
//
2.求1~n的和
public static int sumInt(int x){
    if(x == 1){
        return 1;
    }
    int count = x+sumInt(x-1);
    return count;
}
3.按顺序输出一个整数
public static void inPut(int n) {
    if(n>9){
        inPut(n/10);
    }
    System.out.println(n%10);
}
4.输出斐波那契数列
public static void feiBoNa(int x){

    int i1 = 1;
    int i2 = 1;
    int i3 = 0;
    
    System.out.print(1 +" " +1 + " ");
    
    for(int i = 3;i <= x;i++){
        i3 = i1 +i2;
        i2 = i1;
        i1 = i3;

        System.out.print(i3 + " ");
    }
}

总结:
递归是一种重要的变成解决问题的方式,有一些问题就是用递归定义的,如斐波那契数列用递归就很容易解决。有些问题用循环或者递归都能解决,那么则推荐使用循环解决,相比于递归,循环更高效。

以下是一个可能的Java实现: ```java import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; public class RentPlanGenerator { private static final double RENT_INCREASE_RATE = 0.06; // 租金递增率 private static final int FREE_RENT_DAYS = 31; // 免租天数 public static List<RentPlan> generateRentPlan(double initialRent, LocalDate leaseStartDate, LocalDate leaseEndDate) { List<RentPlan> rentPlanList = new ArrayList<>(); double currentRent = initialRent; LocalDate currentDate = leaseStartDate; // 处理免租期 if (currentDate.isBefore(leaseStartDate.plusDays(FREE_RENT_DAYS))) { currentDate = leaseStartDate.plusDays(FREE_RENT_DAYS); } while (currentDate.isBefore(leaseEndDate)) { LocalDate nextIncreaseDate = currentDate.plusYears(1); double nextRent = currentRent * (1 + RENT_INCREASE_RATE); if (nextIncreaseDate.isBefore(leaseStartDate.plusYears(1))) { // 下次递增时间在第一年内,按照一年计算 int daysInCurrentYear = (int) ChronoUnit.DAYS.between(currentDate, nextIncreaseDate); rentPlanList.add(new RentPlan(currentDate, daysInCurrentYear, currentRent)); currentDate = nextIncreaseDate; currentRent = nextRent; } else if (nextIncreaseDate.isBefore(leaseEndDate)) { // 下次递增时间在第一年外,按照下次递增时间与租赁结束时间的间隔计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } else { // 下次递增时间在租赁结束时间之后,按照租赁结束时间计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } } return rentPlanList; } public static void main(String[] args) { LocalDate leaseStartDate = LocalDate.of(2021, 3, 1); LocalDate leaseEndDate = LocalDate.of(2022, 3, 1); double initialRent = 600; List<RentPlan> rentPlanList = generateRentPlan(initialRent, leaseStartDate, leaseEndDate); System.out.printf("%-12s%-12s%-12s%n", "时间", "天数", "租金"); for (RentPlan rentPlan : rentPlanList) { System.out.printf("%-12s%-12d%-12.2f%n", rentPlan.getStartDate(), rentPlan.getDays(), rentPlan.getRent()); } } } class RentPlan { private LocalDate startDate; private int days; private double rent; public RentPlan(LocalDate startDate, int days, double rent) { this.startDate = startDate; this.days = days; this.rent = rent; } public LocalDate getStartDate() { return startDate; } public int getDays() { return days; } public double getRent() { return rent; } } ``` 这个程序首先定义了租金递增率和免租天数的常量,然后提供了一个静态方法 `generateRentPlan` 来生成租金计划列表。该方法接受三个参数:初始月租金、租赁开始时间和租赁结束时间。 具体实现时,我们使用循环来逐月生成租金计划。在每次循环中,我们首先计算下次递增租金的时间和金额。然后根据下次递增时间与租赁开始时间的间隔,决定本次循环处理的天数和租金金额。最后将这些信息保存到一个 `RentPlan` 对象中,并添加到租金计划列表中。 在主函数中,我们使用 `generateRentPlan` 方法生成租金计划列表,并以表格形式输出。输出结果如下: ``` 时间 天数 租金 2021-04-01 30 600.00 2021-05-01 31 636.00 2021-06-01 30 674.16 2021-07-01 31 713.57 2021-08-01 31 754.29 2021-09-01 30 796.39 2021-10-01 31 840.94 2021-11-01 30 887.02 2021-12-01 31 934.72 2022-01-01 31 984.12 2022-02-01 28 1035.30 ``` 可以看到,程序正确地根据递增周期和递增率生成了每个月的租金计划,并且考虑了免租期的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matinal_01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值