我无法让我的程序打印出闰年

时间:2018-03-31 22:16:30

标签: java if-statement methods boolean conditional

我试图打印出我的程序的最后一种方法似乎无法实现它。我不知道该怎么做。我在网上上课,所以没有人可以问我。我的老师需要三天的时间来回复。

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");

  }

    }
}

4 个答案:

答案 0 :(得分:1)

您永远不会致电displayResuls(isLeap(year), year)

修改:您的displayResults声明也是错误的,它应该是int year而不是boolean year

答案 1 :(得分:0)

您的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年 - 不是。

答案 2 :(得分:0)

这是您重写的代码。就像其他人说的那样,你从未打电话给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");

        }
    }
}

答案 3 :(得分:0)

对您的程序进行了一些更改,并且工作正常

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;
        }
    }