我正在尝试运行一个项目,我的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;
}
}
答案 0 :(得分:0)
尝试此代码,使用主要方法之外的变量和函数
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;
}
}
答案 1 :(得分:0)
我一眼就看出一个问题:你的代码中有一些悬空括号。这使得你在main中声明了你可能有或没有想要的东西,因为你没有用'}'关闭它。确保每个{关闭一个}或您的代码会给您一些错误。