闰年 Java 程序

huangapple go评论62阅读模式
英文:

Leap Year Java Program

问题

我正在尝试学习Java当我输入1800这个不是闰年的年份时它显示"闰年"但如果我输入2011这个不是闰年的年份时它显示"不是闰年"

有人可以帮忙吗我刚开始学Java所以我正在学习这些我试图解决的问题如下

> 如果一个年份可以被4整除则它是闰年然而如果这个年份可以被100整除那么只有当它同时被400整除时它才是闰年

import java.util.Scanner;

public class leapyears {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入一个年份!");
        int year = Integer.valueOf(scanner.nextLine());

        if (year % 4 == 0) {

            System.out.println("闰年!");

            if (year % 100 != 0 && year % 400 == 0) {

                System.out.println("闰年!");

            }
        } else {
            System.out.println("不是闰年!");
        }
    }
}
英文:

I'm trying to learn Java. When I enter 1800 which is not a leap year, it says "Leap year" but if I enter 2011 which is not a leap year it says "not leap year".

anyone that can help? I'm new to Java so I'm learning all this. The problem I'm trying to solve is noted below.

> A year is a leap year if it is divisible by 4. However, if the year is
> divisible by 100, then it is a leap year only when it is also
> divisible by 400.

import java.util.Scanner;
    
public class leapyears {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a year!");
        int year = Integer.valueOf(scanner.nextLine());


        if (year % 4 == 0) {

            System.out.println("Leap year!");

            if (year % 100 != 0 && year % 400 == 0) {

                System.out.println("Leap year!");

            }
        } else {
            System.out.println("Not leap year!");
        }
    }
}

答案1

得分: 3

不要重新发明轮子,Java 已经为您做好了!

> java.time.Year::isLeap

import java.time.Year;

public class Main
{
    public static void main(String[] args) {
        System.out.println(Year.of(2020).isLeap());
    }
}
英文:

Don't reinvent the wheel, Java already does it for you!

> java.time.Year::isLeap

import java.time.Year;

public class Main
{
	public static void main(String[] args) {
		System.out.println(Year.of(2020).isLeap());
	}
}

答案2

得分: 2

你可以编写一个简单的方法,同时也方便进行测试。

for (int year : new int[] { 1843, 1900, 2000, 2004, 2200,
			2400 }) {
	System.out.printf("%d是%s闰年。%n", year,
					isLeapYear(year) ? " " : "不");
}
}

// 非世纪年份能被4整除,或者世纪年份能被400整除
public static boolean isLeapYear(int year) {
	return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

你也可以使用库方法来检查你自己的方法。

import java.time.Year;
for (int year = 2000; year <= 3000; year++) {
	if (Year.isLeap(year) != isLeapYear(year)) {
		System.out.println("糟糕 - " + year);
	}
}
英文:

You can write a simple method which also facilitates testing.

for (int year : new int[] { 1843, 1900, 2000, 2004, 2200,
			2400 }) {
	System.out.printf(&quot;%d is%sa leap year.%n&quot;, year,
					isLeapYear(year) ? &quot; &quot; : &quot; not &quot;);
	}
}

// non century years divisible by 4 or century years divisible by 400
public static boolean isLeapYear(int year) {
	return (year % 4 == 0 &amp;&amp; year % 100 != 0) || year % 400 == 0;
}

You can also use the library method to check your own method.

import java.time.Year;
for (int year = 2000; year &lt;= 3000; year++) {
	if (Year.isLeap(year) != isLeapYear(year)) {
		System.out.println(&quot;Oops - &quot; + year);
	}
}


</details>



# 答案3
**得分**: 1

我认为你的条件是不正确的。```year % 100 != 0``` 和 ```year % 400 == 0``` 不能同时满足。请尝试以下内容。

```java
if (year % 400 == 0 || ((year % 4 == 0) && (year % 100 != 0))) {
    //  ^ 这是或     //^ 这是且

    System.out.println("闰年!");
} else {
    System.out.println("非闰年!");
}
英文:

I think your condition is incorrect. year % 100 !=0 and year % 400 ==0 cannot be met at the same time. Try the following.

   if (year % 400 == 0 || ((year % 4 ==0) &amp;&amp; (year %100 != 0)) {
                   //  ^ this is OR      //^ this is AND

        System.out.println(&quot;Leap year!&quot;);
   else{
        System.out.println(&quot;Not leap year!&quot;);
    }

答案4

得分: 1

起初,这并不是代码的问题,而是你不理解闰年程序背后逻辑的问题。(当我开始学习Java时,也是不久前,我也有同样的问题)😄

你问题中的一个重要错误是,在检查是否能被 4 整除之前,你检查了是否能被 100 整除,因为一个能被 4 整除的数字并不总是能被 100 整除,但反过来则成立。

假设你输入的年份是 1800,它能被 4 整除,这意味着 if 语句成立并执行了相应的代码块。(然而这是错误的)

你应该考虑做的是:

  1. 首先检查是否能被 100 整除,然后再检查数字是否能被 4 或 400 整除。

我为你提供了一段代码以便理解:

import java.util.Scanner;

public class LeapYear {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        
        System.out.print("请输入年份:");
        int yr = s.nextInt();
        
        if(yr % 100 == 0)
        {
            if(yr % 400 == 0)
            {
                System.out.println(yr + " 是闰年");
            }
            else
            {
                System.out.println(yr + " 不是闰年");
            }
            
        }

        else if(yr % 4 == 0)
        {
            System.out.println(yr + " 是闰年");
        }
        
        else
        {
            System.out.println(yr + " 不是闰年");
        }
    }

}

希望这能解决你的问题... (:

附言:这是我第一次回答,我对编程非常新。如果我犯了任何错误,请随时告诉我,如果你想对我现有的代码进行任何更新,也欢迎你这样做...感谢你抽出时间阅读我的回答。

英文:

for starters, this is not a problem with the code, this is the problem of you not understanding the logic behind the leap year program.(when i started doing java, which was not so long ago I had the same problem too):D`

the important flaw that u had in your problem is this
you checked for the modulus of 4 before checking it for 100, coz a number divisible by 4 wont always be divisible by 100 but the converse is true though.

let's say the year you entered was 1800, it will be divisible by 4, which means the if statement is true and the the block is executed.(this is wrong tho)

what you should consider doing is

  1. check for the divisibility of 100 first and after that you can check if the number is divisible by 4 or 400

I've pinned a code for you to understand

import java.util.Scanner;

public class leapyear {

public static void main(String[] args) {

	Scanner s = new Scanner(System.in);
	
	System.out.print(&quot;Enter the year:&quot;);
	int yr = s.nextInt();
	
	if(yr%100==0)
	{
		if(yr%400==0)
		{
			System.out.println(yr+&quot; &quot;+&quot;is a leap year&quot;);
		}
		else
		{
			System.out.println(yr+&quot; &quot;+&quot;is not a leap year&quot;);
		}
		
	}

	else if(yr%4==0)
	{
		System.out.println(yr+&quot; &quot;+&quot;is an leap year&quot;);
	}
	
	else
	{
		System.out.println(yr+&quot; &quot;+&quot;is not a leap year&quot;);
	}
}

}

hope this solves your problem....(:

p.s
this is my first answer and Im very new to programming. So if i made any mistakes feel free to lemme know and if you want to make any update to my existing code you are most welcomed..thank you for taking your time to read this answer of mine

huangapple
  • 本文由 发表于 2020年9月8日 22:49:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63796413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定