每日一练------判断是一年的第几天
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;
}
}
运行结果如下图
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实现输入某年某月某日,判断这一天是这一年的第几天?
- 题目:输入某年某月某日,判断这一天是这一年的第几天?
- 【程序4】输入某年某月某日,判断这一天是这一年的第几天?
- 输入某年某月某日,判断这一天是这一年的第几天
- C语言:输入某年某月某日,判断这一天是这一年的第几天?
- 【程序3】题目:输入某年某月某日,判断这一天是这一年的第几天?
- 输入日期并判断这一天是这一年的第几天
- 定义一个结构体变量(包括年月日)。计算某日在一年中是第几天?(注意还要有闰年的判断)
- 输入某年某月某日,判断这一天是这一年的第几天?
- 输入某年某月某日,判断这一天是这一年的第几天?
- 判断一年中的第几天
- Python编程判断这天是这一年第几天的方法示例
- Java:输入某年某月某日,判断这一天是这一年的第几天?
- 日期与时间处理二(给定一个年月日判断这一天是一年中的第几天)
- C:根据日期判断是一年的第几天、星期几
- Java输入某年某月某日,判断这一天是这一年的第几天?
- js获取当前时间是一年中的第几天&&判断闰年&&当前时间的前后几天
- 给定一个年月日,判断这一天是这一年的第几天
- 输入某年某月某日,判断这一天是这一年的第几天?