0

每次我编译它都说这段代码有 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 . . .
4

1 回答 1

2

您必须将所有这些方法放在类声明中。Commands我相信。您的错误表明您缺少class关键字和声明。(或interfaceenum;但在您的情况下,它是一个;相信我)。

    /**
     * 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
         }
    }

额外的:

  • 在 Java 中声明类
  • 小心丢失或多余的括号。
  • 如果一切都失败了,那就从头开始。构建一个新类,并逐个复制方法,确保您的语法正确。

如您所知,这类问题通常会在 StackOverflow 上得到不好的评价。

于 2013-09-26T22:24:56.100 回答