2013-03-01 136 views
-3

我需要帮助制作一个程序,该程序需要年份和月份的用户输入(例如2012 3)并输出其中的天数,但是,它必须通过确定它是否为闰年来完成这样它就可以知道月份有多少天了,所以它不能预编程,它必须自行计算。并且当输入无效月份(仅限1-12)或输入负整数/小数时,它必须提示重新输入。我不知道如何开始这个!计算一个月中的天数基于闰年?

我也开始否则我不会问,

的System.out.println( “请输入年份和月份:”);

if (stdin.hasNextInt()) { 
     yes = true; 
    int year = stdin.nextInt(); 
    int month = stdin.nextInt(); 
    } 
     else { 
      System.out.println("Invalid Input. ");} 

    if (yes = true); 

    } 
} 

我不知道如何得到它采取了两个数字作为单独的INT(2012年3),以及如何,如果其无效或一年,如果其无效....

+6

我建议在开始你的任务,当你遇到一个简洁的,具体的问题,询问关于它的堆栈溢出。 – Vulcan 2013-03-01 00:54:31

回答

1

有拒绝一个月你必须检查两件事情告诉如果一年是闰年:

  1. 如果年份能被4整除但不能被100整除则是闰年。

  2. 如果年份可以被100整除,也可以被400整除,那么它就是闰年。

-Code它uppppp

(还只是谷歌是ISH下一次 - >“如何判断一个年份是闰年”)

1

让我们写一个函数,它的

公众诠释NUMBEROFDAYS(INT年,月整型){

大多数月份的年份和月份(1..12)数量,并返回给定月份的天数有天,每一个固定数量年,因此:

if (month==1||month==3||month==5||month==7|| 
    month==8||month==10||month==12) return 31; 
if (month==4||month==6||month==9||month==11) return 30; 

此时(如果该函数尚未返回),该月份为2月份,或者该月份无效。让要是下个月是无效的恢复明显的无效值:

if (month!=2) return -1; 

现在,它变得有趣,因为二月有闰年为29天,28天否则。自1582年引入公历年以来,一年的定义是4的倍数,但100年的倍数不是闰年,除非它们也是400的倍数(即1600,2000和2004年是闰年; 1900年和2003年没有)。

if (year>1582) { 
if (month%4==0&&(month%100!=0||month%400==0)) return 29; else return 28; 
} 

在1582年之前,朱利安历法有效。根据儒略历,每年可以被4整除的是闰年。

else { 
//julian calendar 
if (month%4==0) return 29; else return 28; 
} 

现在代码调用numberOfDays

int days; 
do { 
System.out.println("blah blah blah"); 
int year = stdin.nextInt(); 
int month = stdin.nextInt(); 
days = numberOfDays(year,month); 
} while (days<0);