调用方法时遇到问题。例如,我希望以下程序有两种方法:
我无法调用main
中的方法。
import java.util.Scanner;
public class LeapYearCheck {
public static void main(String[] args) {
LeapYearCheck ob = new LeapYearCheck();
ob.isLeapYear();
ob.daysInMonth();
}
static void isLeapYear() {
Scanner input = new Scanner(System.in);
int month = input.nextInt();
System.out.println("Enter a year: ");
int year = input.nextInt();
if (year % 4 == 0 || year % 400 == 0) {
System.out.println(year + " is leap year:");
} else {
System.out.println(year + " is not leap year:");
}
}
static void daysInMonth() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a month :");
int month = input.nextInt();
int year = 0;
if (month == 2) {
System.out.println("There are 29 days in February: " + year + " year");
} else if (month == 1) {
System.out.println("The are 31 days in January " + year + " year");
} else if (month == 2) {
System.out.println("The are 28 days in February " + year + " year");
} else if (month == 3) {
System.out.println("The are 31 days in March " + year + " year");
} else if (month == 4) {
System.out.println("The are 30 days in April " + year + " year");
} else if (month == 5) {
System.out.println("The are 31 days in May " + year + " year");
} else if (month == 6) {
System.out.println("The are 30 days in June " + year + " year");
} else if (month == 7) {
System.out.println("The are 31 days in July " + year + " year");
} else if (month == 8) {
System.out.println("The are 31 days in August " + year + " year");
} else if (month == 9) {
System.out.println("The are 30 days in September " + year + " year");
} else if (month == 10) {
System.out.println("The are 31 days in October " + year + " year");
} else if (month == 11) {
System.out.println("The are 30 days in November " + year + " year");
} else if (month == 12) {
System.out.println("The are 31 days in December " + year + " year");
} else {
System.out.println("Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
}
答案 0 :(得分:5)
这些方法被声明为“静态”。在Java中,这意味着该方法可用于类,而不适用于类的对象。
要明确:我理解你的问题是,当调用第二种方法时,第一种方法的年份不再可用,对吧?
那是因为你没有将它存储在实例变量中,而是存储在方法的本地变量中。方法完成后,局部变量就消失了。而是为您的类创建一个实例变量,例如“私人一年;”。在第一种方法中,然后使用“this.year = ...”为变量赋值。在第二种方法中,使用“this.year”来访问它。
答案 1 :(得分:4)
您的代码将编译并运行(我刚刚尝试过),但您不应该通过以下表达式调用静态方法:
LeapYearCheck ob = new LeapYearCheck();
ob.isLeapYear();
ob.daysInMonth();
你应该 使这些实例方法或将它们称为静态方法,可选择使用类型名称对它们进行限定:
LeapYearCheck.isLeapYear(); // Explicit
daysInMonth(); // Implicit
将静态方法调用为实例方法会导致混淆 - 看起来就像它取决于实例一样,但事实并非如此。
接下来的奇怪之处在于:
static void isLeapYear() {
Scanner input = new Scanner(System.in);
int month = input.nextInt();
System.out.println("Enter a year: ");
您在等待用户输入但没有告诉用户原因 - 然后您无视月份(这是有道理的,因为它与确定一年是闰年还是一年无关不)。摆脱这条线:
int month = input.nextInt();
此外,这个逻辑被破坏了:
if (month == 2) {
System.out.println("There are 29 days in February: " + year + " year");
} else if (month == 1) {
System.out.println("The are 31 days in January " + year + " year");
} else if (month == 2) {
System.out.println("The are 28 days in February " + year + " year");
然后是daysInMonth
方法:
目前基本上代码有点混乱,但是你无法调用main
方法的问题应该不是一个问题......
Calendar
(和它的子类)或Joda Time开头。
答案 2 :(得分:0)
这应该运行。也许不正确,但它会运行。也许你指的是你在main方法中对两个方法调用的警告。你看,你的两种方法都是静态的。静态方法不需要调用对象实例。然而,你在LeapYearCheck实例上调用它们。这是允许的(它将委托给适当的类),只是没有必要。