初学java,在做<java2编程21天>的课后题的时候,有一个要求显示规定年的所有日期的题目(那本书的第五天课后第一题):我txt写的一段如下代码,用javac编译过后,执行java Ayear, 结果只显示出一月份的所有日期....class Ayear{
public static void main(String[] arguments){
//String[] str = new String[366];
int i = 1, j = 1, t, year = 2000;
if(arguments.length > 0)
year = Integer.parseInt(arguments[0]);
for(; i<=12; i++){
t = countDays(i, year);
for(; j<= t; j++){
System.out.println(i + "/" + j + "/" + year);
}
}
}static int countDays(int month, int year) {
int count = -1;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count = 31;
break;
case 4:
case 6:
case 9:
case 11:
count = 30;
break;
case 2:
if (year % 4 == 0)
count = 29;
else
count = 28;
if ((year % 100 == 0) & (year % 400 != 0))
count = 28;
}
return count;
}
而稍微修改一下main变为如下,cmd中就可以显示到12月份了...请教大虾此间有啥区别?public static void main(String[] arguments){
//String[] str = new String[366];
int i = 1, j , t, year = 2000;
if(arguments.length > 0)
year = Integer.parseInt(arguments[0]);
for(; i<=12; i++){
t = countDays(i, year);
for(j =1 ; j<= t; j++){
System.out.println(i + "/" + j + "/" + year);
}
}
}
public static void main(String[] arguments){
//String[] str = new String[366];
int i = 1, j = 1, t, year = 2000;
if(arguments.length > 0)
year = Integer.parseInt(arguments[0]);
for(; i<=12; i++){
t = countDays(i, year);
for(; j<= t; j++){
System.out.println(i + "/" + j + "/" + year);
}
}
}static int countDays(int month, int year) {
int count = -1;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count = 31;
break;
case 4:
case 6:
case 9:
case 11:
count = 30;
break;
case 2:
if (year % 4 == 0)
count = 29;
else
count = 28;
if ((year % 100 == 0) & (year % 400 != 0))
count = 28;
}
return count;
}
而稍微修改一下main变为如下,cmd中就可以显示到12月份了...请教大虾此间有啥区别?public static void main(String[] arguments){
//String[] str = new String[366];
int i = 1, j , t, year = 2000;
if(arguments.length > 0)
year = Integer.parseInt(arguments[0]);
for(; i<=12; i++){
t = countDays(i, year);
for(j =1 ; j<= t; j++){
System.out.println(i + "/" + j + "/" + year);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货