三目运算判断闰年

  • 由用户输入任意一个年份,能被4整除但不能被100整除,或者能被400整除,是闰年。(结果:输出闰年或平年)
  • 三目运算符,其格式为:表达式?语句1:语句2;
  • import java.util.Scanner;
    
    public class Year {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		System.out.println("请输入一个年份:");
    		int year = scan.nextInt();
    		String Y;
    		if(year<=0){
    		    System.out.println("输入年份有误,请输入一个大于0的数");
    		}
    		Y = (year % 4 ==0 && year % 100 != 0 || year % 400 == 0) ? (String) "闰年" : (String) "平年";
    		System.out.println(year + "年是" + Y + "!");
    	}
    }



猜你喜欢

转载自blog.csdn.net/rabbit_judy/article/details/79128988
今日推荐