出于某种原因,我收到一个语法错误,上面写着“语法错误,插入”}“以完成ClassBody”。我检查了每个方法,每个语句以确保我有完整的开始和结束括号,所以我不知道为什么会发生这种情况。谁能告诉我为什么我会遇到这个问题?
将代码复制到另一个文件中并不能解决问题,也不会转到Project>干净。
import java.util.Scanner;
public class jloneman_Numerology
{
private String[] report;
private int day, month, year, num;
public jloneman_Numerology()
{
introduction();
report = new String[9];
num = 0;
}
public void introduction()
{
System.out.println("Welcome to ACME Numerology Reports! We will " +
"determine your special\nnumerology report based on your " +
"birth date.\n");
}
public void getDate()
{
char slash1, slash2;
do
{
System.out.print("Please enter your birth date (mm / dd / yyyy): ");
Scanner in = new Scanner(System.in);
String date = in.nextLine();
month = in.nextInt();
day = in.nextInt();
year = in.nextInt();
slash1 = date.charAt(3);
slash2 = date.charAt(8);
} while (validDate(slash1, slash2) == false);
calcNum();
}
public boolean validDate(char slash1, char slash2)
{
boolean isValid = true;
// Check for valid month
if (month < 1 || month > 12)
{
isValid = false;
System.out.printf("Invalid month: %d\n", month);
}
// Check for valid day
if (day < 1 || day > 31)
{
isValid = false;
System.out.printf("Invalid day: %d\n", day);
}
// Check for months with 30 days, else 31 days = invalid
if ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30))
{
isValid = false;
System.out.printf("Invalid day: %d\n", day);
}
else if (day < 1 || day > 31)
{
isValid = false;
System.out.printf("Invalid day: %d\n", day);
}
// Check for valid year
if (year < 1880 || year > 2280)
{
isValid = false;
System.out.println("Please enter a valid year between 1880 and 2280.");
}
// Check for correct separating character
if (slash1 != '/' || slash2 != '/')
{
isValid = false;
System.out.println("Invalid separating character, please use forward slashes");
}
if (leapYear() == true)
{
if (month == 2 && day > 29)
{
isValid = false;
System.out.printf("Invalid day for 2/%d: %d", year, day);
}
}
return isValid;
}
public boolean leapYear()
{
boolean isLeap;
if (year % 4 == 0 && year % 400 != 0)
isLeap = false;
else
isLeap = true;
return isLeap;
}
public void calcNum()
{
// Separate each digit of the date and add to a single number
// Test number for debugging
num = 5;
}
public void printReport()
{
report[0] = ":1: ";
report[1] = ":2: ";
report[2] = ":3: ";
report[3] = ":4: ";
report[4] = ":5: ";
report[5] = ":6: ";
report[6] = ":7: ";
report[7] = ":8: ";
report[8] = ":9: ";
System.out.println(report[num]);
}
}
78,0-1 Bot
答案 0 :(得分:3)
尝试删除(或注释掉)一种方法,然后查看问题是否仍然存在。如果是,请删除或注释掉其他方法,依此类推,直到错误消失。然后恢复除最后一个方法之外的一切
如果错误没有重新出现,问题可能出在最后一个方法中。
如果 重新出现,则问题更加微妙;也许是嵌入代码中的控制字符。尝试将代码复制并粘贴到纯文本编辑器中(因此任何控件字符都将被忽略,保存并重新编译。
答案 1 :(得分:0)
我也有这个错误。 上面的代码中没有错误(当我使用上面的代码作为子类时)。 我通过清理IDE的工作区并确保调用者正常工作来纠正错误。
答案 2 :(得分:-1)
我也遇到过类似的问题,通过删除类文件然后重新创建文件并粘贴相同的代码来解决它。成功了。