我如何循环年和月以显示下面的输出?显示期间的限制是当前月份和年份此外,显示 3 年,包括截至日期的年份。意思是如果现在是 2019 年,则显示 2018 年和 2017 年。
我尝试使用一些代码作为 Java 应用程序运行,希望得到下面的预期输出,但这是我已经尝试过并得到的。
如果有人能在这里阐明一些问题,将不胜感激。
public class TestClass {
public static void main(String[] args) {
Calendar today = Calendar.getInstance();
//month=5, index starts from 0
int month = today.get(Calendar.MONTH);
//year=2019
int year = today.get(Calendar.YEAR);
for(int i = 0; i < 3; i++) { //year
for(int j= 0; j <= month; j++) { //month
System.out.println("Value of year: " + (year - 2)); //start from 2017 and iterate to 2 years ahead
System.out.println("Value of month: " + (month + 1)); //start from January (index val: 1) and iterate to today's month
}
}
}
}
预期输出:
2017 1 2017 2 2017 3 2017 4 2017 5 2017 6 2017 7 2017 8 2017 9 2017 10 2017 11 2017 12
2018 1 2018 2 2018 3 2018 4 2018 5 2018 6 2018 7 2018 8 2018 9 2018 10 2018 11 2018 12
2019 1 2019 2 2019 3 2019 4 2019 5 2019 6
烙印99
慕神8447489
喵喔喔
相关分类