本帖最后由 duanqichao 于 2016-11-18 01:38 编辑
这原来本来是个C语言的题目,所以按照C的思路折腾了一晚上,基本是这样,不知道大家还有其他的意见没?
[Java] 纯文本查看 复制代码 package com.example.day1;
import java.util.Scanner;
class Temp {
private static int[] month = {1,2,3,4,5,6,7,8,9,10,11,12} ;
private static int[] days1 = {31,29,31,30,31,30,31,31,30,31,30,31};
private static int[] days2 = {31,28,31,30,31,30,31,31,30,31,30,31};
private static Scanner sc = new Scanner(System.in) ;
public static void main(String[] args) {
System.out.println("请输入年份");
int year = sc.nextInt();
System.out.println("请输入天数");
int day = sc.nextInt() ;
boolean b = isLeap(year);
int realyMonth = whatMonth(day, b);
int realyDay = whatDay(day, realyMonth, b);
System.out.println(realyMonth);
System.out.println(realyDay);
System.out.println("这一天是"+year+"年"+realyMonth+"月" + realyDay +"日");
}
// 判断是否是闰年
private static boolean isLeap (int year ){
if( year % 4 == 0 || year % 400 ==0 ){
return true ;
}else{
return false ;
}
}
// 判断输入的天数在哪个月份
private static int whatMonth(int day ,boolean isLeap) {
int temp = 0 ;
if (isLeap){
if (day >0 && day <=31){
temp = month[0];
}else if (day >= 32 & day <= 60){
temp = month[1];
}else if (day >= 61 & day <= 91){
temp = month[2];
}else if (day >= 92 & day <= 121){
temp = month[3];
}else if (day >= 122 & day <= 152){
temp = month[4];
}else if (day >= 153 & day <= 182){
temp = month[5];
}else if (day >= 183 & day <= 213){
temp = month[6];
}else if (day >= 214 & day <= 244){
temp = month[7];
}else if (day >= 245 & day <= 274){
temp = month[8];
}else if (day >= 275 & day <= 305){
temp = month[9];
}else if (day >= 306 & day <= 335){
temp = month[10];
}else {
temp = month[11];
}
}else {
if (day >0 && day <=31){
temp = month[0];
}else if (day >= 32 & day <= 59){
temp = month[1];
}else if (day >= 60 & day <= 90){
temp = month[2];
}else if (day >= 91 & day <= 120){
temp = month[3];
}else if (day >= 121 & day <= 151){
temp = month[4];
}else if (day >= 152 & day <= 181){
temp = month[5];
}else if (day >= 182 & day <= 212){
temp = month[6];
}else if (day >= 213 & day <= 243){
temp = month[7];
}else if (day >= 244 & day <= 273){
temp = month[8];
}else if (day >= 274 & day <= 304){
temp = month[9];
}else if (day > 305 & day <= 334){
temp = month[10];
}else {
temp = month[11];
}
}
return temp ;
}
//判断所在的月份里的第多少天
private static int whatDay(int day,int mon,boolean isleap){
//计算截止上月累计天数
int sum = 0 ;
if(isleap){
for ( int i = 0 ; i < mon-1 ; i++){
sum += days1[i];
}
}else{
for ( int i = 0 ; i < mon-1 ; i++){
sum += days2[i];
}
}
//用实际多少天减去上月累计天数就是本月实际日期
int realDay = day - sum ;
return realDay ;
}
}
|