免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1648 | 回复: 0
打印 上一主题 下一主题

java输入年月日判断是那年的第几天 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-26 13:54 |只看该作者 |倒序浏览
输入年月日判断是一年中的第几天
实验课时做的  ,看到有人交流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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP