计算Java中的闰年 [英] Calculate leap year in Java
问题描述
可能重复:
Java代码计算闰年,这个代码是否正确?
这是家庭作业,我已经收到了成绩,但我没有在我的代码中实现闰年。这是一个简单的程序,显示基于用户输入的一个月中的数字。唯一的事情是我不能弄清楚是一个实现一个闰年,将获得29天的二月而不是28,没有写多个 if
语句。当然有一个更容易的方法?下面是代码:
//显示一个月中的天数
package chapter_3;
import java.util.Scanner;
public class Chapter_3 {
public static void main(String [] args){
System.out.println( \\\
+一个月的天数基于\\\
+您的输入,当提示时请输入\\\
+的数值你想要的月份\\\
+查看包括年份例如:如果你想要\\\
+查看三月进入3 2011等等。
扫描仪输入= new Scanner(System.in);
//提示用户输入月份和年份
System.out.print(请输入月份和年份:);
int month = input.nextInt();
int year = input.nextInt();
if(month == 1){
System.out.println(January+ year +has 31 days);
}
else if(month == 2){
System.out.println(February+ year +have 28 days);
}
else if(month == 3){
System.out.println(March+ year +has 31 days);
}
else if(month == 4){
System.out.println(April+ year +has 30 days);
}
else if(month == 5){
System.out.println(May+ year +has 31 days);
}
else if(month == 6){
System.out.println(June+ year +has 30 days);
}
else if(month == 7){
System.out.println(July+ year +has 31 days);
}
else if(month == 8){
System.out.println(August+ year +has 31 days);
}
else if(month == 9){
System.out.println(September+ year +has 30 days);
}
else if(month == 10){
System.out.println(October+ year +has 30 days);
}
else if(month == 11){
System.out.println(November+ year +has 30 days);
}
else if(month == 12){
System.out.println(December+ year +has 31 days);
}
else {
System.out.println(请输入您要查看的月份的数值
+);
}
}
}
如果您使用 GregorianCalendar
,您可以按照以下方式进行:
确定给定年份是否为闰年。如果给定的
年是闰年,则返回true。要指定BC年数,1年数必须给出
。例如,年份BC 4指定为-3。
GregorianCalendar cal = new GregorianCalendar );
if(cal.isLeapYear(year))
{
System.out.print(给定年份是闰年。
}
else
{
System.out.print(给定年份不是闰年。
}
Possible Duplicate:
Java Code for calculating Leap Year, is this code correct?
This was homework, and I have already received my grade, but I did not implement leap years in my code. This is a simple program that displays the numbers in a month based on user input. The only thing is I can't figure out is a way to implement a leap year that would get 29 days for February rather than 28, without writing multiple if
statements. Surely there is an easier way? Here is the code:
//Displays number of days in a month
package chapter_3;
import java.util.Scanner;
public class Chapter_3 {
public static void main(String[] args) {
System.out.println("This program will calculate \n"
+ "the number of days in a month based upon \n"
+ "your input, when prompted please enter \n"
+ "the numerical value of the month you would like\n"
+ "to view including the year. Example: if you would like\n"
+ "to view March enter 3 2011 and so on.");
Scanner input = new Scanner(System.in);
//Prompt user for month and year
System.out.print("Please enter the month and year: ");
int month = input.nextInt();
int year = input.nextInt();
if (month == 1) {
System.out.println("January " + year + " has 31 days");
}
else if (month == 2) {
System.out.println("February " + year + " has 28 days");
}
else if (month == 3) {
System.out.println("March " + year + " has 31 days");
}
else if (month == 4) {
System.out.println("April " + year + " has 30 days");
}
else if (month == 5) {
System.out.println("May " + year + " has 31 days");
}
else if (month == 6) {
System.out.println("June " + year + " has 30 days");
}
else if (month == 7) {
System.out.println("July " + year + " has 31 days");
}
else if (month == 8) {
System.out.println("August " + year + " has 31 days");
}
else if (month == 9) {
System.out.println("September " + year + " has 30 days");
}
else if (month == 10) {
System.out.println("October " + year + " has 30 days");
}
else if (month == 11) {
System.out.println("November " + year + " has 30 days");
}
else if (month == 12) {
System.out.println("December " + year + " has 31 days");
}
else {
System.out.println("Please enter the numerical value "
+ "of the month you would like to view");
}
}
}
If you use the GregorianCalendar
, you could do as below
Determines if the given year is a leap year. Returns true if the given year is a leap year. To specify BC year numbers, 1 - year number must be given. For example, year BC 4 is specified as -3.
GregorianCalendar cal = new GregorianCalendar();
if(cal.isLeapYear(year))
{
System.out.print("Given year is leap year.");
}
else
{
System.out.print("Given year is not leap year.");
}
这篇关于计算Java中的闰年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!