#千峰JAVA逆战班,3月26日#

在前锋学习的第11天;
JAVA_DAY9;
今天学习的课程有函数的递归,数组等知识;
中国加油!世界加油!
我自己加油!

class TestYearAndMonth
{
	//判定给定的year是否是闰年,如果是返回true,否则返回false
	public static boolean leapYear(int year1){
		if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
			System.out.println("是闰年啊");
			return true;
		}else{
			System.out.println("不是润年,是平年。。");
			return false;
		}
	}
	//返回给定year这个年里,month这个月的天数
	public static int getDaysOfMonth(int year2,int month){//year=2020,month=2
		int days = 0;//用于表示month这个月的天数
		switch(month){
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				days = 31;
				break;

			case 4:
			case 6:
			case 9:
			case 11:
				days = 30;
				break;

			case 2:
				if(leapYear(year2)){//boolean类型
					days = 29;
				}else{
					days = 28;
				}
				break;
		}
		return days;
	}
	public static void main(String[] args) 
	{
		//练习1:设计一个方法,用于判断给定的年份是否是闰年
		//练习2:设计一个方法,用于返回给定年份中这个月份的天数
		//1,3,5,7,8,10,12-->31天
		//4,6,9,11-->30天
		//2,--->29/28

		boolean res1 = leapYear(2020);
		System.out.println(res1);

		int res2 = getDaysOfMonth(2030,2);
		System.out.println(res2);

		System.out.println("Hello World!");

		/*
		两个方法给的形参算是局部变量,可以重名不影响
		*/
	}
}

public class Test1Recursion
{
	//递归方法
	public static int getSum(int n){
		System.out.println("******");
		if(n == 1){
			return 1;
		}else{
			return getSum(n - 1) + n;
		}
	}
	public static void main(String[] args){
		/*
		求1-5的和:
			+1+2+3+4+5  
			+1+2+3+4		+5
		求1-n的和:getSum(n),该方法用于表示求1-n的和。
				   getSum(5)
				   :getSum(4) + 5
						 :getSum(3) + 4
							:getSum(2) + 3
								getSum(1) + 2
			
									1
		*/
		int res = getSum(5);
		System.out.println(res);
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
	}
}
发布了11 篇原创文章 · 获赞 3 · 访问量 389

猜你喜欢

转载自blog.csdn.net/yuxinganggame/article/details/105134147
今日推荐