您的位置:首页 > 编程语言 > Java开发

每日一练------判断是一年的第几天

2014-04-03 17:34 232 查看
import java.util.Calendar;

import java.util.Date;

import java.util.Scanner;

/*

 * 题目:输入某年某月某日,判断这一天是这一年的第几天?

 * 虽然写的代码不精简,但是逻辑简单,易懂

 */

public class Basic14 {

 public static void main(String[] args) {

  Basic14 my = new Basic14();

  my.judge();

 }

 

 public void judge()

 { 

  System.out.println("请输入年,月,日");

  Scanner sc =new Scanner(System.in);

  int year=0;

  int month=0;

  int day=0;

  try {

   year = sc.nextInt();

   month = sc.nextInt();

   day = sc.nextInt();

  } catch (Exception e) {

   // TODO Auto-generated catch block

   System.out.println("输入有误");

   judge();

  }

  int month1 = 31;

  int month2 = 28;

  int month3 = 31;

  int month4 = 30;

  int month5 = 31;

  int month6 = 30;

  int month7 = 31;

  int month8 = 31;

  int month9 = 30;

  int month10 = 31;

  int month11 = 30;

  int month12 = 31;

  if(isRunnian(year))

  {

   month2 = 29;

  }

  int result = 0;

  switch(month)

  {

   case 1:

    result = day;break;

   case 2:

    result = month1+day;break;

   case 3:

    result = month1+month2+day;break;

   case 4:

    result = month1+month2+month3+day;break;

   case 5:

    result = month1+month2+month3+month4+day;break;

   case 6:

    result = month1+month2+month3+month4+month5+day;break;

   case 7:

    result = month1+month2+month3+month4+month5+month6+day;break;

   case 8:

    result = month1+month2+month3+month4+month5+month6+month7+day;break;

   case 9:

    result = month1+month2+month3+month4+month5+month6+month7+month8+day;break;

   case 10:

    result = month1+month2+month3+month4+month5+month6+month7+month8+month9+day;break;

   case 11:

    result = month1+month2+month3+month4+month5+month6+month7+month8+month9+month10+day;break;

   case 12:

    result = month1+month2+month3+month4+month5+month6+month7+month8+month9+month10+month11+day;break;   

  }

  

  System.out.println("是"+year+"年的第"+result+"天");

  judge();

  

 }

 public static boolean isRunnian(int year)

 {

  boolean result = false;

  if((year%400==0)||(((year%4)==0)&&((year%100)!=0)))

  {

   result=true;

  }

  return result;

 }

}

运行结果如下图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java