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

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;
 }

}
运行结果如下图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值