Java little exercise:. Determine how many days the month is based on the month entered

Source: Internet
Author: User

package practicego;import java.util.scanner;/* * 2. Based on the month entered, determine how many days of the month  */public class cto {public static void main (String[]  args)  {scanner sc = new scanner (system.in); System.out.println ("Enter month:"); Int month = sc.nextint ();switch  (month)  {case 1:case  3:case 5:case 7:case 8:case 10:case 12:system.out.println (month+ "  Month  " + "  has 31 days. "); Break;case 4:case 6:case 9:case 11:system.out.println (month+"   month   "+"   has 30 days. Case 2:system.out.println ("Please enter the Year:"); Int year = sc.nextint ();//judgment is not a leap years if  (year%4==0  && year%100!=0 | |  year%400==0)  {month = 29;} else {month = 28;} System.out.println ("February  " + "  with  " +month+ "); Break;default:system.out.println (" Will you lose? You're not going to lose! "), Break;}} 

Test:

Enter Month: November has 31 days.
Input Month: 2 Please enter year: 20,162 months with 29 days
Input Month: 2 Please enter year: 20,172 months with 28 days
Enter the month: 33 months with 31 days.


Java little exercise:. Determine how many days the month is based on the month entered

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.