Java 我能';我不能让我的程序打印闰年
我试图让它打印出我的程序的最后一种方法似乎无法让它这样做。我不知道该怎么办。我在一个在线课堂上,所以没有人可以问我。我的老师花了三天的时间来回答Java 我能';我不能让我的程序打印闰年,java,if-statement,methods,boolean,conditional,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 dis
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");
}
}
}
您从不调用
显示结果(isLeap(year),year)
编辑:您对displayResults的声明也是错误的,它应该是
int year
而不是boolean year
您的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-leap,但2100-not。这是您重写的代码。正如其他人所说,您从未调用过
displaysresults
,并且有一些小的打字错误。继续编码;)
对您的程序进行了一些更改,效果良好
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;
}
}
“年”应该是什么?您将其声明为“int”,然后尝试将其作为布尔值传递,然后再次将其作为int进行处理…?:-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;
}
}