【发布时间】:2021-11-26 01:40:44
【问题描述】:
public class Array {
public static void main(String args[]) {
int[] daysInMonth = new int[31];
for (int i = 0; i < 31; i++) {
System.out.println("Day of the month: " + daysInMonth[i]);
}
}
}
我知道我可以硬编码每个月的每一天的值。我可以这样做:daysInMonth[0] = 1;对于当月的所有值,但为了提高效率想使用循环来代替。我将如何创建一个循环来输出“每月的天数:1,每月的天数:2,等等?”
【问题讨论】:
-
所以您只想将值
1输出到31?听起来你可以只输出i + 1的值,然后daysInMonth数组不会增加任何值。 -
如果可以打印
i+1,为什么还要使用数组?