请教各位大侠,改怎么改,菜鸟才刚刚开始学java。非常感谢!
public static void main(String[] args) {
// TODO Auto-generated method stub
int year,lemp;
int month=3,day=12;
int i;
switch(month){
case 1:i=0;break;
case 2:i=30;break;
case 3:i=58;break;
case 4:i=89;break;
case 5:i=119;break;
case 6:i=150;break;
case 7:i=180;break;
case 8:i=211;break;
case 9:i=242;break;
case 10:i=273;break;
case 11:i=303;break;
case 12:i=334;break;
//default;
}
i=i+day;
if(year%400==0||(year%100!=0&&year%4==0)){ //判断是否为闰年
lemp=1;
}else
lemp=0;
if(lemp==1&&month>2){ // 如果是闰年且月份大于二,总天数应该加一
i++;
}
System.out.println("这一天是以年中的第:"+i+"天!");
}
}
问题,输入某年某月某日,判断这一天是这一年的第几天?(这是我用java写的代码如下:)
答案:3 悬赏:0 手机版
解决时间 2021-02-20 03:55
- 提问者网友:末路
- 2021-02-19 04:11
最佳答案
- 五星知识达人网友:天凉才是好个秋
- 2021-02-19 05:29
public static void main(String[] args) {
// TODO Auto-generated method stub
int year = 2012, lemp;
int month = 2, day = 21;
int i = 0;
switch (month) {
case 1:
i = 0;
break;
case 2:
i = 31;
break;
case 3:
i = 59;
break;
case 4:
i = 90;
break;
case 5:
i = 120;
break;
case 6:
i = 151;
break;
case 7:
i = 181;
break;
case 8:
i = 212;
break;
case 9:
i = 243;
break;
case 10:
i = 273;
break;
case 11:
i = 304;
break;
case 12:
i = 334;
break;
// default;
}
i = i + day;
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { // 判断是否为闰年
lemp = 1;
} else
lemp = 0;
if (lemp == 1 && month > 2) { // 如果是闰年且月份大于二,总天数应该加一
i++;
}
System.out.println("这一天是以年中的第:" + i + "天!");
}
注意一月是31天哦!!!
按照你的思路可以这样改,但是还有别的更好办法
// TODO Auto-generated method stub
int year = 2012, lemp;
int month = 2, day = 21;
int i = 0;
switch (month) {
case 1:
i = 0;
break;
case 2:
i = 31;
break;
case 3:
i = 59;
break;
case 4:
i = 90;
break;
case 5:
i = 120;
break;
case 6:
i = 151;
break;
case 7:
i = 181;
break;
case 8:
i = 212;
break;
case 9:
i = 243;
break;
case 10:
i = 273;
break;
case 11:
i = 304;
break;
case 12:
i = 334;
break;
// default;
}
i = i + day;
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { // 判断是否为闰年
lemp = 1;
} else
lemp = 0;
if (lemp == 1 && month > 2) { // 如果是闰年且月份大于二,总天数应该加一
i++;
}
System.out.println("这一天是以年中的第:" + i + "天!");
}
注意一月是31天哦!!!
按照你的思路可以这样改,但是还有别的更好办法
全部回答
- 1楼网友:長槍戰八方
- 2021-02-19 07:03
完整修改如下:
public static void main(String[] args) {
int year = 2011, lemp;
int month = 3, day = 11;
int i = 0;
switch (month) {
case 1:
i = 0;
break;
case 2:
i = 30;
break;
case 3:
i = 58;
break;
case 4:
i = 89;
break;
case 5:
i = 119;
break;
case 6:
i = 150;
break;
case 7:
i = 180;
break;
case 8:
i = 211;
break;
case 9:
i = 242;
break;
case 10:
i = 273;
break;
case 11:
i = 303;
break;
case 12:
i = 334;
break;
// default;
}
i = i + day;
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {//判断是否为闰年
lemp = 1;
} else
lemp = 0;
if (lemp == 1 && month > 2) {// 如果是闰年且月份大于二,总天数应该加一
i++;
}
System.out.println("这一天是以年中的第:"+i+"天!");
}
- 2楼网友:酒安江南
- 2021-02-19 06:50
import java.text.simpledateformat;
import java.util.calendar;
import java.util.scanner;
public class getdate {
public static void main(string[] args) {
scanner scanner = new scanner(system.in);
system.out.println("请输入年:");
int year = scanner.nextint();
system.out.println("请输入月:");
int month = scanner.nextint();
system.out.println("请输入日:");
int day = scanner.nextint();
calendar calendar1 = calendar.getinstance();
calendar1.set(year, 0, 1);
long time1 = calendar1.gettimeinmillis();
calendar calendar2 = calendar.getinstance();
calendar2.set(year, month - 1, day);
long time2 = calendar2.gettimeinmillis();
long result = (time2 - time1) / 1000 / 60 / 60 / 24;
simpledateformat format = new simpledateformat("yyyy/mm/dd");
system.out.println(format.format(calendar2.gettime()) + "距离"
+ format.format(calendar1.gettime()) + "有" + result + "天");
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯