The required output is false, true, false, true. My results are false, true, true, true. Is there something missing in my code?

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

The required output is false, true, false, true. My results are false, true, true, true. Is there something missing in my code?

问题

这是情景。

写一个名为isLeapYear的方法,带有一个名为year的int类型参数。

参数的值需要大于等于1且小于等于9999。

如果参数不在该范围内,则返回false。

否则,如果在有效范围内,计算年份是否是闰年,如果是闰年则返回true,否则返回false。

要判断一个年份是否是闰年,请按照以下步骤进行:

  1. 如果年份可以被4整除,转到步骤2。
  2. 否则,转到步骤5。
  3. 如果年份可以被100整除,转到步骤3。
  4. 否则,转到步骤4。
  5. 如果年份可以被400整除,转到步骤4。
  6. 否则,转到步骤5。
  7. 该年是闰年(有366天)。方法isLeapYear需要返回true。
  8. 该年不是闰年(有365天)。方法isLeapYear需要返回false。

以下年份不是闰年:
1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600
因为它们可以被100整除,但不能被400整除。

以下年份是闰年:
1600, 2000, 2400
因为它们既可以被100整除,又可以被400整除。

输入/输出示例:

  • isLeapYear(-1600); → 应返回false,因为参数不在范围内(1-9999)

  • isLeapYear(1600); → 应返回true,因为1600是闰年

  • isLeapYear(2017); → 应返回false,因为2017不是闰年

  • isLeapYear(2000); → 应返回true,因为2000是闰年

这是我的代码:

  1. public static boolean isLeapYear(int year) {
  2. boolean result;
  3. if (year >= 1 && year <= 9999) {
  4. result = true;
  5. } else {
  6. result = false;
  7. }
  8. boolean step;
  9. if (year % 4 == 0) {
  10. if (year % 100 == 0) {
  11. if (year % 400 == 0) {
  12. step = result;
  13. }
  14. } else {
  15. step = result;
  16. }
  17. } else {
  18. step = false;
  19. }
  20. return step;
  21. }
英文:

This is the scenario.

Write a method isLeapYear with a parameter of type int named year.

The parameter needs to be greater than or equal to 1 and less than or equal to 9999.

If the parameter is not in that range return false.

Otherwise, if it is in the valid range, calculate if the year is a leap year and return true if it is a leap year, otherwise return false.

To determine whether a year is a leap year, follow these steps:

  1. If the year is evenly divisible by 4, go to step
  2. Otherwise, go to step 5.2. If the year is evenly divisible by 100, go to step
  3. Otherwise, go to step 4.3. If the year is evenly divisible by 400, go to step
  4. Otherwise, go to step 5.4. The year is a leap year (it has 366 days). The method isLeapYear needs to return true.
  5. The year is not a leap year (it has 365 days). The method isLeapYear needs to return false.

The following years are not leap years:
1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600
This is because they are evenly divisible by 100 but not by 400.

The following years are leap years:
1600, 2000, 2400
This is because they are evenly divisible by both 100 and 400.

Examples of input/output:

  • isLeapYear(-1600); → should return false since the parameter is not in range (1-9999)

  • isLeapYear(1600); → should return true since 1600 is a leap year

  • isLeapYear(2017); → should return false since 2017 is not a leap year

  • isLeapYear(2000); → should return true because 2000 is a leap year

This is my code:

  1. public static void isLeapYear(int year) {
  2. boolean result;
  3. if (year &gt;= 1 &amp;&amp; year &lt;= 9999) {
  4. System.out.println(result = true);
  5. } else {
  6. System.out.println(result = false);
  7. }
  8. boolean step;
  9. if (year == (year % 4)) {
  10. if (year == (year % 100)) {
  11. step = result;
  12. }
  13. }
  14. if (year == (year % 100)) {
  15. if (year == (year % 400)) {
  16. step = result;
  17. }
  18. }
  19. if (year == (year % 400)) {
  20. step = result;
  21. } else {
  22. step = result;
  23. }
  24. }

答案1

得分: 1

  1. public static void main(String[] args) throws IOException {
  2. isLeapYear(-1600);// → 应返回 false,因为参数不在范围内(1-9999)
  3. isLeapYear(1600); ////→ 应返回 true,因为 1600 是闰年
  4. isLeapYear(2017);// → 应返回 false,因为 2017 不是闰年
  5. isLeapYear(2000);
  6. isLeapYear(99999);
  7. }
  8. public static void isLeapYear(int year) {
  9. // 应在 1 到 9999 之间
  10. // 如果年份可以被 4 整除,进入步骤 2...
  11. // 如果年份可以被 100 整除,进入步骤 3...
  12. // 如果年份可以被 400 整除,进入步骤 4...
  13. // 该年是闰年(有 366 天)。
  14. // 该年不是闰年(有 365 天)。
  15. boolean isLeap = false;
  16. if(year >= 1 && year <= 9999 && year % 4 == 0){
  17. if( year % 100 == 0){
  18. if ( year % 400 == 0)
  19. isLeap = true;
  20. else
  21. isLeap = false;
  22. }else
  23. isLeap = true;
  24. }else {
  25. isLeap = false;
  26. }
  27. if(isLeap==true)
  28. System.out.println(year + " 是闰年。");
  29. else
  30. System.out.println(year + " 不是闰年。");
  31. }
  32. }

输出

-1600 不是闰年。
1600 是闰年。
2017 不是闰年。
2000 是闰年。
99999 不是闰年。

英文:

I have made some changes and merged ur conditions

  1. public static void main(String[] args) throws IOException {
  2. isLeapYear(-1600);// → should return false since the parameter is not in range (1-9999)
  3. isLeapYear(1600); ////→ should return true since 1600 is a leap year
  4. isLeapYear(2017);// → should return false since 2017 is not a leap year
  5. isLeapYear(2000);
  6. isLeapYear(99999);
  7. }
  8. public static void isLeapYear(int year) {
  9. //Should lie between 1 &amp;&amp; 9999
  10. //If the year is evenly divisible by 4, go to step 2. ...
  11. //If the year is evenly divisible by 100, go to step 3. ...
  12. //If the year is evenly divisible by 400, go to step 4. ...
  13. //The year is a leap year (it has 366 days).
  14. //The year is not a leap year (it has 365 days).
  15. boolean isLeap = false;
  16. if(year &gt;= 1 &amp;&amp; year &lt;= 9999 &amp;&amp; year % 4 == 0){
  17. if( year % 100 == 0){
  18. if ( year % 400 == 0)
  19. isLeap = true;
  20. else
  21. isLeap = false;
  22. }else
  23. isLeap = true;
  24. }else {
  25. isLeap = false;
  26. }
  27. if(isLeap==true)
  28. System.out.println(year + &quot; is a Leap Year.&quot;);
  29. else
  30. System.out.println(year + &quot; is not a Leap Year.&quot;);
  31. }
  32. }

output

  1. -1600 is not a Leap Year.
  2. 1600 is a Leap Year.
  3. 2017 is not a Leap Year.
  4. 2000 is a Leap Year.
  5. 99999 is not a Leap Year.

答案2

得分: 1

  1. public class LeapYear {
  2. public static boolean isLeapYear(int year) {
  3. if (year >= 1 && year <= 9999) {
  4. if (year % 4 == 0) {
  5. if (year % 100 == 0) {
  6. if (year % 400 == 0) {
  7. return true;
  8. } else {
  9. return false; // year%400==0 else
  10. }
  11. } else {
  12. return true; // year%100==0 else
  13. }
  14. } else {
  15. return false; // year%4==0 else
  16. }
  17. } else {
  18. return false; // this else associated with upper if
  19. }
  20. }
  21. }
英文:
  1. public class LeapYear {
  2. public static boolean isLeapYear(int year){
  3. if (year&gt;=1 &amp;&amp; year&lt;=9999)
  4. { if (year%4==0){
  5. if (year%100==0){
  6. if (year%400==0){
  7. return true;
  8. }else {
  9. return false; //year%400==0 else
  10. }
  11. }else {
  12. return true; //year%100==0 else
  13. }
  14. }else {
  15. return false; // year%4==0 else
  16. }
  17. }
  18. else return false; //this else associated with upper if
  19. }
  20. }

huangapple
  • 本文由 发表于 2020年10月12日 16:41:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64314421.html
匿名

发表评论

匿名网友

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

确定