- 论坛徽章:
- 0
|
输入年月日判断是一年中的第几天
实验课时做的 ,看到有人交流C的实现 我觉得用switch() 会简洁一点 未加入验证
没有日期的验证 比如说 13月33号 有时间再弄
import java.io.*;
public class SuanTianShu{
static public void main(String[] args){
int year,month,day;
int days =0;
boolean luinian = false;
System.out.print("input the year:");
year = IO.getInt();
System.out.print("input the month:");
month = IO.getInt();
System.out.print("input the day:");
day = IO.getInt();
if(year%400 ==0 || (year%4==0&&year%100!=0)){
luinian = true;
}
for(int i = 1;imonth;i++){
switch(i){
case 1:case 3:case 5:case 7:case 8:case 10:case 12: days += 31;break;
case 2: days += 28;break;
case 4:case 6:case 9:case 11: days += 30;break;
}
}
if(luinian&&month>2){
days++;
}
days+=day;
System.out.println(year+"年"+month+"月"+day+"日是"+year+"的第"+days+"天");
}
}
class IO{
public static int getInt(){
DataInputStream dis=new DataInputStream(System.in);
int value=0;
try{
String str=dis.readLine();
value=Integer.parseInt(str);
}catch(IOException e){
e.printStackTrace();
}
return value;
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/64540/showart_508751.html |
|