Trinocular operation to determine leap year

  • Any year input by the user that is divisible by 4 but not divisible by 100, or divisible by 400, is a leap year. (result: output leap year or common year)
  • Ternary operator, its format is: expression? statement1: statement2;
  • import java.util.Scanner;
    
    public class Year {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Please enter a year: ");
    		int year = scan.nextInt();
    		String Y;
    		if(year<=0){
    		    System.out.println("The year entered is incorrect, please enter a number greater than 0");
    		}
    		Y = (year % 4 ==0 && year % 100 != 0 || year % 400 == 0) ? (String) "闰年" : (String) "平年";
    		System.out.println(year + "年是" + Y + "!");
    	}
    }



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325833538&siteId=291194637