Java 与私有变量相关的表达式的非法开始

Java 与私有变量相关的表达式的非法开始,java,variables,int,private,Java,Variables,Int,Private,我试图运行一个项目,我的AP计算机科学老师提供给我们检查我们的工作。我已经试着上了两堂课,想让老师的代码正常工作,但是没有用。问题是,当初始化私有变量n时,读取表达式的非法开始时会出现错误。任何帮助都将不胜感激。 代码如下: package eastersunday; public class Easter { public static void main(String[] args) { private int n; private int p; /**

我试图运行一个项目,我的AP计算机科学老师提供给我们检查我们的工作。我已经试着上了两堂课,想让老师的代码正常工作,但是没有用。问题是,当初始化私有变量n时,读取表达式的非法开始时会出现错误。任何帮助都将不胜感激。 代码如下:

 package eastersunday;

    public class Easter
{
public static void main(String[] args) 
{
   private int n;
   private int p;

   /**
      Constructs the date of Easter Sunday.
     * @param year
   */
   public Easter(int year)
   {
      int y = year;
      int a = y % 19;
      int b = y / 100;
      int c = y % 100;
      int d = b / 4;
      int e = b % 4;
      int g = (8 * b + 13) / 25;
      int h = (19 * a + b - d - g + 15) % 30;
      int j = c / 4;
      int k = c % 4;
      int m = (a + 11 * h) / 319;
      int r = (2 * e + 2 * j - k - h + m + 32) % 7;
      n = (h - m + r + 90) / 25;
      p = (h - m + r + n + 19) % 32;
   }

   /**
      Gets the month of Easter Sunday
      @return month of Easter Sunday
   */
   public int getEasterSundayMonth()
   {
      return n;
   }

   /**
      Gets the date of Easter Sunday
      @return date of Easter Sunday
   */
   public int getEasterSundayDay()
   {
      return p;
   }
}

尝试这段代码,在main方法之外使用变量和函数


我一眼就能看出一个问题:代码中有一些悬空的括号。这使得您可以在main中声明一些东西,这可能是您的本意,也可能不是您的本意,因为您没有用“}”关闭它。确保每个{以}结束,否则您的代码将给您一些错误

您正在主方法中声明字段和方法。在Java中不能这样做。旁注:考虑给变量起一个有意义的名字。
package eastersunday;

    public class Easter
{
public static void main(String[] args) 
{
}
   private int n;
   private int p;

   /**
      Constructs the date of Easter Sunday.
     * @param year
   */
   public Easter(int year)
   {
      int y = year;
      int a = y % 19;
      int b = y / 100;
      int c = y % 100;
      int d = b / 4;
      int e = b % 4;
      int g = (8 * b + 13) / 25;
      int h = (19 * a + b - d - g + 15) % 30;
      int j = c / 4;
      int k = c % 4;
      int m = (a + 11 * h) / 319;
      int r = (2 * e + 2 * j - k - h + m + 32) % 7;
      n = (h - m + r + 90) / 25;
      p = (h - m + r + n + 19) % 32;
   }

   /**
      Gets the month of Easter Sunday
      @return month of Easter Sunday
   */
   public int getEasterSundayMonth()
   {
      return n;
   }

   /**
      Gets the date of Easter Sunday
      @return date of Easter Sunday
   */
   public int getEasterSundayDay()
   {
      return p;
   }
}