从另一个方法调用方法(java)

从另一个方法调用方法(java),java,boolean,Java,Boolean,几周前,我开始自学java,我一直遇到同样的基本问题,我不能从同一个类中的另一个方法调用一个方法。我要么得到一个“symbol not found”错误(我认为是因为该方法超出了范围,要么该方法不再工作。下面的代码是创建日历类程序的一部分。我将使用//in代码注释指出问题的确切位置 public class MyDate { private int year; private int month; private int day; private static String[] strMonth

几周前,我开始自学java,我一直遇到同样的基本问题,我不能从同一个类中的另一个方法调用一个方法。我要么得到一个“symbol not found”错误(我认为是因为该方法超出了范围,要么该方法不再工作。下面的代码是创建日历类程序的一部分。我将使用//in代码注释指出问题的确切位置

public class MyDate {
private int year;
private int month;
private int day;
private static String[] strMonths = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

private static String[] strDays = {"Sunday", "Monday", "Tuesday", 
"Wednesday", "Thursday", "Friday", "Saturday"};

private static int[] daysInMonths = {31, 28, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

public static boolean isLeapYear(int year) { // This is the first method, which works fine 
// when being called from the main method.
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
        return true;
    } else {
        return false;
    }
    };

public static boolean isLeapYear; // I put this declaration in, because I got 
// "symbol not found" errors, when referencing the method from the second method. 
// I'm guessing it partially invalidates the first declaration.
public static boolean isValidDate(int year, int month, int day) { // The second method
    if ((year >= 1 ) && (year <= 9999)){
        if ((month >= 0) && (month <= 11)) {
            if ((day >= 1) && ((month == 0) || (month == 2) || (month == 4) || (month == 6) 
|| (month == 7) || (month == 9) || (month == 11)) && (day <= 31)) {
                return true;
            }  
            else if ((day >= 1) && ((month == 3) || (month == 5) || (month == 8) 
|| (month == 10) && (day <= 30))) {
                return true;
            } 
            else if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){ 
// Code from the first method (above), which I would like to replace with just a reference 
// to the first method (for instance (isLeapYear = true)), 
// but it doesn't work the same as the code above (or at all).
                if ((month == 1) && (day == 29)) {
                    return true;
                } 
                else
                    if ((day >= 1) && ((month == 1) && (day <= 28))) {
                    return true;
                    } 
                    else {
                        return false;
                        }



            }           
        }
    }
    return isValidDate; }

感谢您的帮助!

从代码中删除
public static boolean isLeapYear;
,每次需要调用函数时,在本例中,
public static boolean isLeapYear(int year){..}
必须使用括号让编译器知道您实际上在那里调用函数

在这种情况下,您应该使用
isLeapYear(year)


然后
if(isLeapYear(year)){..}else{..}
给定的
MyApplication
的包名(在下面出现的每个地方替换您的包名)
导入
isLeapYear
,如下所示

package MyApplication;

public class MyDate {

public static boolean isLeapYear(int year) { // This is the first method, which works fine 
// when being called from the main method.
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
        return true;
    } else {
        return false;
    }
    };

}
主要类别:

package MyApplication;

import static MyApplication.MyDate.isLeapYear;

public class NewClass{

  public static void main(String[] args) throws Exception 
  {
    System.out.println(isLeapYear(1900));  // false
    System.out.println(isLeapYear(2000));  // true
    System.out.println(isLeapYear(2011));  // false
    System.out.println(isLeapYear(2012));     
  }
}

isLeapYear(year)
是您如何调用它的。请显示实际生成错误的代码。除了从main(这不是问题)之外,您现在所拥有的实际上并不显示对isLeapYear的调用。此外,请在方法定义的末尾保留分号。
package MyApplication;

import static MyApplication.MyDate.isLeapYear;

public class NewClass{

  public static void main(String[] args) throws Exception 
  {
    System.out.println(isLeapYear(1900));  // false
    System.out.println(isLeapYear(2000));  // true
    System.out.println(isLeapYear(2011));  // false
    System.out.println(isLeapYear(2012));     
  }
}