java判断是一年的第几天实例代码

来源:互联网 发布:存在主义 知乎 编辑:程序博客网 时间:2024/04/27 00:33

java程序如何判断是一年的第几天?实例代码介绍。在我们的实际开发中判断一年中的某天是第几天是经常遇到的。那么如何实现这个要求呢?以下是相关的java程序代码案例。

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:

更多内容请访问:http://www.hongyanliren.com/2014m04/5155.html

0 0
原创粉丝点击