Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4460185
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: Java

2008-03-26 13:54:42

输入年月日判断是一年中的第几天

实验课时做的  ,看到有人交流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;i<month;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;
    }
}

阅读(7891) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~