【问题标题】:I can't get my program to print out a Leap year我无法让我的程序打印出闰年
【发布时间】:2018-03-31 22:16:30
【问题描述】:

我试图让它打印出我的程序的最后一种方法似乎无法让它做到这一点。我不知道该怎么办。而且我正在上在线课程,所以我真的没有人可以问。我的老师需要三天时间才能回复。

import java.util.Scanner;

public class LeapYear {

public static void main(String[] args) {
displayInstructions();
int year = getYear();
isLeap(year);
}

public static void displayInstructions() {
    System.out.println("This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year.");
}

public static int getYear() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = reader.nextInt();
return year;
}

public static boolean isLeap(boolean year) {
    boolean isLeapYear = false;
    if (year % 4 == 0 && year != 100) {
        isLeap = true;

    }
    else {
        isLeap false;
    }



}
public static void displayResuls( boolean isLeap, boolean year) {
  if (isLeap)
  {
    System.out.println("Year" +year+" is a Leap Year.");
  }

  else {
  System.out.println("Year" +year+" is not a Leap Year");

  }

    }
}

【问题讨论】:

  • “年”应该是什么?您将其声明为“int”,然后尝试将其作为布尔值传递,然后再次将其作为 int 执行...? :-0

标签: java if-statement methods boolean conditional


【解决方案1】:

你永远不会打电话给displayResuls(isLeap(year), year)

编辑:你的 displayResults 声明也是错误的,它应该是 int year 而不是 boolean year

【讨论】:

    【解决方案2】:

    你的isLeap 函数应该是这样的:

    public static boolean isLeap(int year) {
        boolean isLeapYear = false;
        if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
            isLeapYear = true;
        } else {
            isLeapYear = false;
        }
        return isLeapYear;
    }
    

    我认为在条件下的主要错误。它比你的更复杂。 正确一个:

    (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))
    

    例如,2000 - 飞跃,但 2100 - 不是。

    【讨论】:

      【解决方案3】:

      这是您重写的代码。就像其他人说的,你从来没有打过displayResuls 并且有一些小错别字。继续编码;)

      import java.util.Scanner;
      
      public class LeapYear {
      
          public static void main(String[] args) {
              displayInstructions();
              int year = getYear();
              boolean ans = isLeap(year);
              displayResuls(ans, year);
          }
      
          public static void displayInstructions() {
              System.out.println("This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year.");
          }
      
          public static int getYear() {
              Scanner reader = new Scanner(System.in);
              System.out.println("Enter a year: ");
              int year = reader.nextInt();
              return year;
          }
      
          public static boolean isLeap(int year) {
      
              boolean isLeapYear = false;
      
              if (((year % 4) == 0) && (year != 100)) {
                  isLeapYear = true;
              } else {
                  isLeapYear = false;
              }
              return isLeapYear;
          }
      
          public static void displayResuls(boolean isLeap, int year) {
      
              if (isLeap) {
                  System.out.println("Year " + year + " is a Leap Year.");
              } else {
                  System.out.println("Year " + year + " is not a Leap Year");
      
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        对您的程序进行了一些更改,它运行良好

        public static void main(String[] args) {
                displayInstructions();
                int year = getYear();
                System.out.println("Entered Year is "+isLeap(year));
            }
        
            public static void displayInstructions() {
                System.out.println("This program asks you to enter a year as a 4 digit number. "
                        + "The output will indicate whether the year you entered is a leap year.");
            }
        
            public static int getYear() {
                Scanner reader = new Scanner(System.in);
                System.out.println("Enter a year: ");
                int year = reader.nextInt();
                return year;
            }
        
            public static boolean isLeap(int year) {
                boolean isLeapYear = false;
                if (year%4 == 0 && year != 100) {
                    return isLeapYear = true;
        
                }
                else {
                    return isLeapYear = false;
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-11
          • 1970-01-01
          • 1970-01-01
          • 2020-03-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多