以某种格式打印闰年

时间:2017-07-22 16:22:31

标签: java

我希望以两年的格式打印闰年 ' [2120,2024,2028]'。我希望他们用这些逗号和方括号...请帮助我。在java中 我的计划:

System.out.print("[");
for(int i=2017;i<=2040;i++)
{
    if(i%4==0)
    {
        System.out.print(i);
        if(i!=2040)
        {
            System.out.print(",");
        }       
    }
}
System.out.print("]");

如果我更改值,则逗号无法正常工作

2 个答案:

答案 0 :(得分:0)

在每个数字之前放一个逗号更容易,而不是之后。效果是一样的,但是你只需要把第一个闰年作为特例:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.raja.myproject"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
            multiDexEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')

  testCompile 'junit:junit:4.12'


 compile 'com.android.support:appcompat-v7:23.4.0'

 compile 'com.google.android.gms:play-services-analytics:7.3.0'



 compile 'com.google.android.gms:play-services-maps:9.8.0'



  compile 'com.google.android.gms:play-services-location:9.8.0'



  compile 'com.android.support:multidex:1.0.0'



 compile 'com.google.android.gms:play-services-appindexing:9.8.0'

}

答案 1 :(得分:0)

闰年有更多规则。

我建议你研究这个方法并在你的代码中使用它:

boolean isLeapYear(year) {
    if (year % 400 == 0) {
        return true;
    } else {
        if ((year % 4 == 0) && (year % 100 != 0)) {
            return true;
        } else {
            return false;
        }
    }
}