每次编译它都说我在这段代码中有21个错误?我真的被困在这一点上......我已经尝试了一切。如果有人可以帮助我会很棒。
以下是代码:
//Normal-Player End
public static void archiveLogs(Player player, String[] cmd) {
try {
if (player.getRights() < 1)
return;
String location = "";
if (player.getRights() >= 5) {
location = "data/logs/admin" + player.getUsername() + ".txt";
} else if (player.getRights() == 4) {
location = "data/logs/mod" + player.getUsername() + ".txt";
}
String afterCMD = "";
for (int i = 1; i < cmd.length; i++)
afterCMD += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
BufferedWriter writer = new BufferedWriter(new FileWriter(location,
true));
writer.write("[" + now("dd MMMMM yyyy 'at' hh:mm:ss z") + "] - ::"
+ cmd[0] + " " + afterCMD);
writer.newLine();
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String now(String dateFormat) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(cal.getTime());
}
private Commands() {
}
}
以下是我的编译器所说的内容:
writer.flush();
^
src\com\rs\game\player\content\Commands.java:2502: error: class, interface, or e
num expected
writer.close();
^
src\com\rs\game\player\content\Commands.java:2503: error: class, interface, or e
num expected
} catch (IOException e) {
^
src\com\rs\game\player\content\Commands.java:2505: error: class, interface, or e
num expected
}
^
src\com\rs\game\player\content\Commands.java:2515: error: class, interface, or e
num expected
public static String now(String dateFormat) {
^
src\com\rs\game\player\content\Commands.java:2517: error: class, interface, or e
num expected
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
^
src\com\rs\game\player\content\Commands.java:2518: error: class, interface, or e
num expected
return sdf.format(cal.getTime());
^
src\com\rs\game\player\content\Commands.java:2519: error: class, interface, or e
num expected
}
^
Note: src\com\rs\game\player\Inventory.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
21 errors
compiling Dialogues
src\com\rs\game\player\content\Commands.java:2477: error: illegal start of type
return true;
^
src\com\rs\game\player\content\Commands.java:2477: error: ';' expected
return true;
^
src\com\rs\game\player\content\Commands.java:2483: error: class, interface, or e
num expected
public static void archiveLogs(Player player, String[] cmd) {
^
src\com\rs\game\player\content\Commands.java:2487: error: class, interface, or e
num expected
String location = "";
^
src\com\rs\game\player\content\Commands.java:2488: error: class, interface, or e
num expected
if (player.getRights() >= 5) {
^
src\com\rs\game\player\content\Commands.java:2490: error: class, interface, or e
num expected
} else if (player.getRights() == 4) {
^
src\com\rs\game\player\content\Commands.java:2492: error: class, interface, or e
num expected
}
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2496: error: class, interface, or e
num expected
BufferedWriter writer = new BufferedWriter(new FileWrite
r(location,
^
src\com\rs\game\player\content\Commands.java:2498: error: class, interface, or e
num expected
writer.write("[" + now("dd MMMMM yyyy 'at' hh:mm:ss z")
+ "] - ::"
^
src\com\rs\game\player\content\Commands.java:2500: error: class, interface, or e
num expected
writer.newLine();
^
src\com\rs\game\player\content\Commands.java:2501: error: class, interface, or e
num expected
writer.flush();
^
src\com\rs\game\player\content\Commands.java:2502: error: class, interface, or e
num expected
writer.close();
^
src\com\rs\game\player\content\Commands.java:2503: error: class, interface, or e
num expected
} catch (IOException e) {
^
src\com\rs\game\player\content\Commands.java:2505: error: class, interface, or e
num expected
}
^
src\com\rs\game\player\content\Commands.java:2515: error: class, interface, or e
num expected
public static String now(String dateFormat) {
^
src\com\rs\game\player\content\Commands.java:2517: error: class, interface, or e
num expected
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
^
src\com\rs\game\player\content\Commands.java:2518: error: class, interface, or e
num expected
return sdf.format(cal.getTime());
^
src\com\rs\game\player\content\Commands.java:2519: error: class, interface, or e
num expected
}
^
Note: src\com\rs\game\player\Inventory.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
21 errors
compiled Main Server Folder
src\com\rs\game\player\content\Commands.java:2477: error: illegal start of type
return true;
^
src\com\rs\game\player\content\Commands.java:2477: error: ';' expected
return true;
^
src\com\rs\game\player\content\Commands.java:2483: error: class, interface, or e
num expected
public static void archiveLogs(Player player, String[] cmd) {
^
src\com\rs\game\player\content\Commands.java:2487: error: class, interface, or e
num expected
String location = "";
^
src\com\rs\game\player\content\Commands.java:2488: error: class, interface, or e
num expected
if (player.getRights() >= 5) {
^
src\com\rs\game\player\content\Commands.java:2490: error: class, interface, or e
num expected
} else if (player.getRights() == 4) {
^
src\com\rs\game\player\content\Commands.java:2492: error: class, interface, or e
num expected
}
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2496: error: class, interface, or e
num expected
BufferedWriter writer = new BufferedWriter(new FileWrite
r(location,
^
src\com\rs\game\player\content\Commands.java:2498: error: class, interface, or e
num expected
writer.write("[" + now("dd MMMMM yyyy 'at' hh:mm:ss z")
+ "] - ::"
^
src\com\rs\game\player\content\Commands.java:2500: error: class, interface, or e
num expected
writer.newLine();
^
src\com\rs\game\player\content\Commands.java:2501: error: class, interface, or e
num expected
writer.flush();
^
src\com\rs\game\player\content\Commands.java:2502: error: class, interface, or e
num expected
writer.close();
^
src\com\rs\game\player\content\Commands.java:2503: error: class, interface, or e
num expected
} catch (IOException e) {
^
src\com\rs\game\player\content\Commands.java:2505: error: class, interface, or e
num expected
}
^
src\com\rs\game\player\content\Commands.java:2515: error: class, interface, or e
num expected
public static String now(String dateFormat) {
^
src\com\rs\game\player\content\Commands.java:2517: error: class, interface, or e
num expected
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
^
src\com\rs\game\player\content\Commands.java:2518: error: class, interface, or e
num expected
return sdf.format(cal.getTime());
^
src\com\rs\game\player\content\Commands.java:2519: error: class, interface, or e
num expected
}
^
21 errors
compiled Main Server Folder
src\com\rs\game\player\content\Commands.java:2477: error: illegal start of type
return true;
^
src\com\rs\game\player\content\Commands.java:2477: error: ';' expected
return true;
^
src\com\rs\game\player\content\Commands.java:2483: error: class, interface, or e
num expected
public static void archiveLogs(Player player, String[] cmd) {
^
src\com\rs\game\player\content\Commands.java:2487: error: class, interface, or e
num expected
String location = "";
^
src\com\rs\game\player\content\Commands.java:2488: error: class, interface, or e
num expected
if (player.getRights() >= 5) {
^
src\com\rs\game\player\content\Commands.java:2490: error: class, interface, or e
num expected
} else if (player.getRights() == 4) {
^
src\com\rs\game\player\content\Commands.java:2492: error: class, interface, or e
num expected
}
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2494: error: class, interface, or e
num expected
for (int i = 1; i < cmd.length; i++)
^
src\com\rs\game\player\content\Commands.java:2496: error: class, interface, or e
num expected
BufferedWriter writer = new BufferedWriter(new FileWrite
r(location,
^
src\com\rs\game\player\content\Commands.java:2498: error: class, interface, or e
num expected
writer.write("[" + now("dd MMMMM yyyy 'at' hh:mm:ss z")
+ "] - ::"
^
src\com\rs\game\player\content\Commands.java:2500: error: class, interface, or e
num expected
writer.newLine();
^
src\com\rs\game\player\content\Commands.java:2501: error: class, interface, or e
num expected
writer.flush();
^
src\com\rs\game\player\content\Commands.java:2502: error: class, interface, or e
num expected
writer.close();
^
src\com\rs\game\player\content\Commands.java:2503: error: class, interface, or e
num expected
} catch (IOException e) {
^
src\com\rs\game\player\content\Commands.java:2505: error: class, interface, or e
num expected
}
^
src\com\rs\game\player\content\Commands.java:2515: error: class, interface, or e
num expected
public static String now(String dateFormat) {
^
src\com\rs\game\player\content\Commands.java:2517: error: class, interface, or e
num expected
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
^
src\com\rs\game\player\content\Commands.java:2518: error: class, interface, or e
num expected
return sdf.format(cal.getTime());
^
src\com\rs\game\player\content\Commands.java:2519: error: class, interface, or enum expected
}
^
Note: src\com\rs\game\player\Inventory.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
21 errors
Compiled all Successfully
Press any key to continue . . .
答案 0 :(得分:2)
您必须将所有这些方法放在类声明中。我认为Commands
。您的错误尖叫您错过了 class 关键字和声明。 (或界面,或枚举;但在您的情况下,它是类;相信我)。
/**
* Even though this seems to be a static helper class,
* you still need the class declaration. The artefact
* name (Commands.java) won't suffice.
*/
public class Commands { // <--- Eh?
private Commands() {
// Prevent object from being instantiated
}
public static void archiveLogs(Player player, String[] cmd) {
// implementation omitted
}
public static String now(String dateFormat) {
// implementation omitted
}
}
额外:
您知道,这些类型的问题通常会导致StackOverflow上的错误代表。