需要帮助理解这一点并通过我的代码正确解释它吗?

时间:2015-09-16 00:19:26

标签: java if-statement boolean boolean-expression

一个程序,它有一个方法需要int" Year"作为参数,然后根据参数确定它是否是闰年。他们给出了一张表格,显示哪些不是,哪个是闰年

2010/4 = no .. / 100 = no .. / 400 = no .. leap = no

2012/4 = yes .. / 100 = no .. / 400 = no .. leap = yes

1900/4 = yes .. / 100 = yes .. / 400 = no .. leap = no

2000/4 =是.. / 100 =是.. / 400 =是.. leap = yes

他们还希望该方法确定一年是否在格里高利历1565之前

以下是我目前的代码。它可以使用几年,而不适用于其他人。显然我做错了什么。如果有人能告诉我我正在做什么以及我做错了什么或者如何最好地解决这个问题,那么任何帮助都会受到很大的影响?

public class theleapyear{

  public static void main( String [] args){
    leapyear(2010);
    leapyear(2012);
    leapyear(1900);
    leapyear(2000);
    leapyear(1565);
  }

  public static void leapyear( int year){

    if (year < 1565)
      System.out.println( year + ":" + " predates the Gregorian Calendar ");

    else
    if (year % 4 != 0)
      if (year % 100 != 0)
      if (year % 400 != 0)
    {
      System.out.println( year + ":" + " is not a leap year ");
    }
    else
    {
      if (year % 4 == 0)
        if (year % 100 != 0)
        if (year % 400 != 0)
        System.out.println( year + ":" + " is a leap year ");  
    }
    else
    {
      if (year % 4 == 0)
        if (year % 100 == 0)
        if (year % 400 != 0)
        System.out.println( year + ":" + " is not a leap year ");
    }
    else
    {
      if (year % 4 == 0)
        if (year % 100 == 0)
        if (year % 400 == 0)
        System.out.println( year + ":" + " is a leap year ");

    }
  }
}

1 个答案:

答案 0 :(得分:-1)

你重复了很多代码;尝试使用比较器恢复它。

public class theleapyear{

 public static void main( String [] args){
  leapyear(2010);
  leapyear(2012);
  leapyear(1900);
  leapyear(2000);
  leapyear(1565);
}

public static void leapyear( int year){

if (year < 1565)
  System.out.println( year + ":" + " predates the Gregorian Calendar ");

else
{
if ((year%4 != 0){
      System.out.println(year + ":" + " is not a leap year ");

 }

else {
if((year%100==0 && year%400==0)||(year%100!=0 && year%400!=0){
    System.out.println(year + ":" + " it is a leap year ");
}

else {
   System.out.println(year + ":" + " is not a leap year ");    
 }