【问题标题】:"How to solve error: ';' expected in Java?" [duplicate]“如何解决错误:';'期望在 Java 中?” [复制]
【发布时间】:2019-11-30 04:38:15
【问题描述】:

我正在尝试运行我的代码行,但它显示了一个小错误。 我也努力写了\ ; " " 之类的东西,但它不起作用。

public static void message() {
    System.out.println("This program surely cannot");
    System.out.println("have any \"errors")"in it");
}

我希望这个方法可以运行,但它表明:

Error:(9, 48) java: ';' expected.

【问题讨论】:

  • System.out.println("have any \"errors\")\"in it"); 应该可以工作,但不能确保你得到你想要的输出。

标签: java


【解决方案1】:

您需要使用退格符 \ 转义字符串中的所有引号 "

public static void message() {
    System.out.println("This program surely cannot");
    System.out.println("have any \"errors\")\"in it");
}

否则解析器会看到你已经结束了你的字符串,并且当) 后面有一些它不知道如何解释的随机字符时会中断。这只是一个语法错误,编译时会崩溃。

System.out.println("have any \"errors")"in it");
                                     ^^^~~~~~~~~
                                     |||    ^
                                     |||    |
                                     ||| Parser sees these as just random characters
                 String ends here____|||
                                      ||____Here should be a semicolon
                                      |
                                     println( matches this

【讨论】:

    【解决方案2】:
    System.out.println("have any \"errors")"in it");
    

    有一个语法错误:你必须转义引号:

    System.out.println("have any \"errors\")\"in it");
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2013-11-23
      • 2012-05-14
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多