Java练习事例 Posted on 2018-07-29 | In 奇技淫巧 基本的Java输入输出练习 通过键盘输入年份和月份判断这一面是否是闰年,并且输出这一年的这个月份有多少天。 1234567891011121314151617181920212223242526272829303132333435363738394041424344import java.util.Scanner;public class Java_003{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("请输入年份:"); int nYear=scanner.nextInt(); if(nYear<0) { System.out.println("输入有误!请重新输入年份:"); nYear=scanner.nextInt(); } System.out.println("请输入月份:"); int nMonth=scanner.nextInt(); if(nMonth<=0 || nMonth>12) { System.out.println("输入有误!请重新输入月份:"); nMonth=scanner.nextInt(); } int nDays = 30 ; if(nMonth==1 || nMonth==3 || nMonth==5 || nMonth==7 || nMonth==8 || nMonth==10 || nMonth==12) nDays=31; else if(nMonth==2) nDays = 28; if(nYear % 400 == 0 || (nYear % 4 ==0 && nYear % 100 != 0)) { System.out.println(nYear + " 这一年是闰年!"); if(nMonth==2) nDays=29; } else System.out.println(nYear + "年不是闰年!"); System.out.println(nYear + "年"+nMonth+"月有" + nDays + "天"); }} 这是运行结果示意 -------------本文结束感谢您的阅读-------------