标签:
某年份只需要满足下列两个条件之一,即可认定是闰年:
使用三目运算符,构建表示判断结果的 String 类型信息,并输出到控制台。代码如下所示:
1 import java.util.Scanner; 2 public class LeapYear { 3 public static void main(String[] args) { 4 Scanner scanner = new Scanner(System.in); 5 System.out.println("请输入年份(例如:2012)"); 6 int year = scanner.nextInt(); 7 scanner.close(); 8 boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) 9 || year % 400 == 0; 10 String msg = isLeapYear ? year + "是闰年" : year + "不是闰年"; 11 System.out.println(msg); 12 } 13 }
标签:
原文地址:http://www.cnblogs.com/luckyBrown/p/5840459.html