Introduction to the author
Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!
introduction
Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!
Navigation
✪ introduction to Java white 200 case series directory index
◄ previous article 49. return statement in the loop
► next 51. Constellations are calculated according to the date of birth
Leap year judgment
Leap year is one of the following conditions
- Can be divided by 4, but not by 100.
- Can be divided by 400.
Option 1
Self encapsulation method to achieve
package demo.demo50; import java.util.Scanner; public class Run2 { public static void main(String[] args) { //Gets the year entered int year = getYear(); boolean run = isRun(year); if(run){ System.out.println(year + " It's a leap year"); }else { System.out.println(year + " Not a leap year"); } } private static int getYear() { System.out.println("Please enter the year:"); Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } //Determine whether it is a leap year private static boolean isRun(int year){ /*One of the following is leap year * * 1.Can be divided by 4, but not by 100 * 2.Can be divided by 400 */ if((year%4==0&&year%100!=0)||year%400==0){ return true; } return false; } }
Test run 1:
Please enter the year:
2012
2012 is a leap year
Test run 2:
Please enter the year:
2013
2013 is not a leap year
Option 2
Use Gregorian calendar to judge
package demo.demo50; import java.util.GregorianCalendar; import java.util.Scanner; public class Run { public static void main(String[] args) { //Gets the year entered int year = getYear(); boolean run = isRun(year); if(run){ System.out.println(year + " It's a leap year"); }else { System.out.println(year + " Not a leap year"); } } private static int getYear() { System.out.println("Please enter the year:"); Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } //Determine whether it is a leap year private static boolean isRun(int year){ GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); return cal.isLeapYear(year); } }
Test run:
Please enter the year:
2020
2020 is a leap year
Month printing
Add month printing. Note that February in leap year is 29 days and February in normal year is 28 days
package demo.demo50; import java.util.GregorianCalendar; import java.util.Scanner; public class Run3 { public static void main(String[] args) { //Gets the year entered int year = getYear(); boolean run = isRun(year); if(run){ System.out.println(year + " It's a leap year"); }else { System.out.println(year + " Not a leap year"); } //Print month days printMonthDay(run); } //Print month days private static void printMonthDay(boolean run) { int day=0; for (int i = 1; i <= 12; i++) { day = getDay(run,i); System.out.println(i+"Days of month:"+day); } } //Gets the number of days in the month private static int getDay(boolean run,int month) { int day=0; switch (month) { //1 \ 3 \ 5 \ 7 \ 8 \ 10 \ 12 31 days case 1: case 3: case 5: case 7: case 8: case 10: case 12: day=31; break; //Leap year to be judged in February case 2: if(run){ day=29; }else { day=28; } break; // 4 \ 6 \ 9 \ 11 30 days case 4: case 6: case 9: case 11: day=30; break; default: break; } return day; } private static int getYear() { System.out.println("Please enter the year:"); Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } //Determine whether it is a leap year private static boolean isRun(int year){ GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); return cal.isLeapYear(year); } }
Test run 1:
Please enter the year:
2020
2020 is a leap year
Days in January: 31
Days in February: 29
Days in March: 31
Days in April: 30
Days in May: 31
Days in June: 30
Days in July: 31
Days in August: 31
Days in September: 30
Number of days in October: 31
Days in November: 30
Days in December: 31
Test run 2:
Please enter the year:
2021
2021 is not a leap year
Days in January: 31
Days in February: 28
Days in March: 31
Days in April: 30
Days in May: 31
Days in June: 30
Days in July: 31
Days in August: 31
Days in September: 30
Number of days in October: 31
Days in November: 30
Days in December: 31
Summary
This section summarizes "Java judges leap years and prints the days corresponding to the month". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning java with brother Xiao Ming, [pay attention to a wave] won't get lost.
Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!
Navigation
✪ introduction to Java white 200 case series directory index
◄ previous article 49. return statement in the loop
► next 51. Constellations are calculated according to the date of birth
Popular column recommendation
1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory