day08
定义一个方法的步骤:
1.考虑方法名 -- 见名知意
2.参数(参数有几个?名字怎么取?参数是什么类型)
3.返回值(返回值是什么类型)
一、方法的重载
1.理解:
方法与方法之间的关系
2.条件:
1.方法必须在同一个类中
2.方法名必须一致
3.参数列表的个数或者类型不一致
4.与返回值无关
3.好处:
系统会根据具体实参类型自动匹配到对应的方法中
4.应用场景:
一个类的多个方法的功能一致,细节实现不一样,就可以考虑使用重载
5.需求:
创建一个方法,传入两个int值,返回最大值 – getMax
创建一个方法,传入三个int值,返回最大值 – getMax
创建一个方法,传入两个double值,返回最大值 – getMax
创建一个方法,传入三个double值,返回最大值 – getMax
public static void main(String[] args){
System.out.println(getMax(10,20,30));
}
public static int getMax(int a,int b){
return (a>b)?a:b;
}
public static int getMax(int a,int b,int c){
int max = getMax(a,b);
max = getMax(max,c);
return max;
}
public static double getMax(double a,double b){
return (a>b)?a:b;
}
public static double getMax(double a,double b,double c){
double max = getMax(a,b);
max = getMax(max,c);
return max;
}
二、方法的递归
1.概念:
方法调用方法自身
public static void main(String[] args){
//错误示范:
//StackOverflowError - 栈内存溢出
//前言:方法里声明的变量叫做局部变量,其作用域就在该方法的内部
//错误出现的原因:调用方法就会在栈内存开辟空间,用于存放该方法的局部变量,
// 死循环的调用方法,很快栈内存就被装满并溢出了
method();
}
public static void method(){
method();
}
2.经验:
1.找规律
2.找出口
3.需求:
(1)设计一个方法,传入int类型的数字n,计算n的阶乘
递归理解图
分析:
5! = 12345; -> 5! = 4! * 5
4! = 1234 -> 4! = 3! * 4
3! = 123 -> 3! = 2! * 3
2! = 12 -> 2! = 1! * 2
1! = 1 -> 1! = 1
找规律:n! = (n-1)! * n;
找出口:1! = 1
public static void main(String[] args){
int num = getFactorial(5);
System.out.println(num);
}
public static int getFactorial(int n){
if(n == 1){
return 1;
}else{
return getFactorial(n-1)*n;
}
}
(2)不死神兔
有一对兔子,从出生后第三个月起每个月都生一对兔子,
小兔子长到第三个月后每个月又生一对兔子,
假如兔子都不死,问第n个月的兔子对数为多少?
分析:
找规律:当月兔子的对数等于上个月+上上个月
找出口:第一个月和第二个月兔子都是一对
public static void main(String[] args){
int num = getRabbit(8);
System.out.println(num);
}
public static int getRabbit(int month){
if(month == 1 || month ==2){
return 1;
}else{
return getRabbit(month-1) + getRabbit(month-2);
}
}
4.万年历 – 流程、思路
需求:输入年和月,打印当月的日历
线索:1900年1月1日是星期一
分析:
1.输入年和月
2.计算出1900年1月1日到输入年输入月的第一天的总天数
2.1 计算1900年(包含)到输入年(排他)的总天数
2.2 计算1月到输入月(排他)的总天数
2.3 合并总天数
3.计算出当月的第一天是星期几
4.计算当月的天数
5.打印日历
public static void main(String[] args){
//1.输入年和月
Scanner scan = new Scanner(System.in);
System.out.println("请输入年:");
int year = scan.nextInt();
System.out.println("请输入月:");
int month = scan.nextInt();
//2.计算出1900年1月1日到输入年输入月的第一天的总天数
//2.1 计算1900年(包含)到输入年(排他)的总天数
int allDayOfYear = 0;
for(int i = 1900;i<year;i++){
if(i%4==0 && i%100!=0 || i%400==0){//闰年
allDayOfYear += 366;
}else{//平年
allDayOfYear += 365;
}
}
//2.2 计算1月到输入月(排他)的总天数
int allDayOfMonth = 0;
for(int i = 1;i<month;i++){
switch(i){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
allDayOfMonth += 31;
break;
case 4:case 6:case 9:case 11:
allDayOfMonth += 30;
break;
case 2:
if(year%4==0 && year%100!=0 || year%400==0){//闰年
allDayOfMonth += 29;
}else{//平年
allDayOfMonth += 28;
}
break;
}
}
//2.3 合并总天数
int allDay = allDayOfYear + allDayOfMonth + 1;//加1是获取自1900年1月1日到输入年输入月的第一天的总天数
//3.计算出当月的第一天是星期几
int week = allDay%7;//0~6
if(week == 0){
week = 7;
}
//4.计算当月的天数
int day = 0;
switch(month){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
day = 31;
break;
case 4:case 6:case 9:case 11:
day = 30;
break;
case 2:
if(year%4==0 && year%100!=0 || year%400==0){//闰年
day = 29;
}else{//平年
day = 28;
}
break;
}
//5.打印日历
System.out.println(year + "年" + month + "月");
System.out.println("一\t二\t三\t四\t五\t六\t日");
int count = 0;//换行的计数器(逢7换行)
//打印空格
for(int i = 1;i<week;i++){
System.out.print("\t");
count++;
}
//打印日期
for(int i = 1;i<=day;i++){
System.out.print(i + "\t");
count++;
if(count % 7 == 0){
System.out.println();//换行
}
}
}
5.方法版本的万年历
public static void main(String[] args){
//1.输入年和月
Scanner scan = new Scanner(System.in);
System.out.println("请输入年:");
int year = scan.nextInt();
System.out.println("请输入月:");
int month = scan.nextInt();
//2.计算出1900年1月1日到输入年输入月的第一天的总天数
int allDay = getAllDay(year,month);
//3.计算星期几
int week = getWeek(allDay);
//4.计算当月的天数
int day = getDay(year,month);
//5.打印日期
printCalendar(year,month,day,week);
}
//打印日历
public static void printCalendar(int year,int month,int day,int week){
System.out.println(year + "年" + month + "月");
System.out.println("一\t二\t三\t四\t五\t六\t日");
int count = 0;//换行的计数器(逢7换行)
//打印空格
for(int i = 1;i<week;i++){
System.out.print("\t");
count++;
}
//打印日期
for(int i = 1;i<=day;i++){
System.out.print(i + "\t");
count++;
if(count % 7 == 0){
System.out.println();//换行
}
}
}
//计算星期
public static int getWeek(int allDay){
int week = allDay%7;
if(week == 0){
week = 7;
}
return week;
}
//计算出1900年1月1日到输入年输入月的第一天的总天数
public static int getAllDay(int year,int month){
int allDay = getAllDayOfYear(year) + getAllDayOfMonth(year,month) + 1;
return allDay;
}
//计算1月到输入月的总天数
public static int getAllDayOfMonth(int year,int month){
int allDayOfMonth = 0;
for(int i = 1;i<month;i++){
allDayOfMonth += getDay(year,i);
}
return allDayOfMonth;
}
//计算1900年到输入年的总天数
public static int getAllDayOfYear(int year){
int allDayOfYear = 0;
for(int i = 1900;i<year;i++){
if(isLeapYear(i)){
allDayOfYear += 366;
}else{
allDayOfYear += 365;
}
}
return allDayOfYear;
}
//获取当月的天数
public static int getDay(int year,int month){
int day = 0;
switch(month){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
day = 31;
break;
case 4:case 6:case 9:case 11:
day = 30;
break;
case 2:
if(isLeapYear(year)){
day = 29;
}else{
day = 28;
}
break;
}
return day;
}
//判断是否是闰年的方法
public static boolean isLeapYear(int year){
if(year%4==0 && year%100!=0 || year%400==0){
return true;
}
return false;
}