gpt4 book ai didi

java - 如何用 Java 打印接下来的 20 个闰年

转载 作者:行者123 更新时间:2023-12-01 09:19:45 24 4
gpt4 key购买 nike

由于我的任务,我想知道如何做到这一点。

package leapyearsassignment;

import java.util.Scanner;

public class LeapYearsAssignment {
public static void main(String[] args) {
int leapyear;
int addyear = 4;
int year;
Scanner in = new Scanner(System.in);

System.out.println("Please enter a year to print the next 20 years.");
year = in.nextInt();
System.out.println( "You are asking for the leap years starting from " + year + ".");

//for loop here?

}

}

这就是我开始想知道如何使用“for 循环”来打印从用户想要的年份开始的接下来的 20 个闰年。

编辑:这是我之前编写的代码:

package leapyearsassignment;

public class LeapYearsAssignment {


public static void main(String[] args) {
int year;

for ( year = 2016; year <= 2092; year = year +4) {
System.out.println(year);}

}

}

但我想要的是用户想要的年份,而不是 2016 年。我对编程相当陌生,几个月前才开始;确切地说是九月。

最佳答案

Scanner sc = new Scanner(System.in);
System.out.println("Enter the year to check for leap year.");
int year = sc.nextInt();

System.out.println("The next 20 leap years from the given year " + year+ " are: ");
int count =0;
while(count != 20){
year = year+4;
if((year%400==0) || (year%4==0 && year%100 != 0)){
System.out.println(year);
}
count++;
}

关于java - 如何用 Java 打印接下来的 20 个闰年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40244171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com