gpt4 book ai didi

java - 语法错误,插入 "}"来完成 ClassBody?

转载 作者:行者123 更新时间:2023-12-02 00:06:25 26 4
gpt4 key购买 nike

由于某种原因,我收到一个语法错误,提示“语法错误,插入“}”以完成 ClassBody。”我检查了每个方法、每个语句,以确保我有完整的左括号和右括号,所以我不知道为什么会发生这种情况。谁能告诉我为什么我会遇到这个问题?

将代码复制到另一个文件中并不能解决问题,转到“项目”>“清理”也不能解决问题。

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

最佳答案

尝试删除(或注释掉)一种方法,看看问题是否仍然存在。如果是这样,请删除或注释掉其他方法,依此类推,直到错误消失。然后恢复除最后一个方法之外的所有内容。

如果错误不再出现,则问题可能出在最后一个方法中。

如果它确实再次出现,则问题更加微妙;也许是嵌入在代码中的控制字符。尝试将代码复制并粘贴到纯文本编辑器中(这样任何控制字符都将被忽略,保存并重新编译。

关于java - 语法错误,插入 "}"来完成 ClassBody?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com