当我尝试使用斜线打印时,为什么System.out.println错误?

时间:2018-10-19 18:31:35

标签: java

您好,所以我们刚从学校开始学习Java,当时我在家里玩。我想知道为什么当我用*制作三角形时有效,但是当我尝试使用'/','\'和'_'使其看起来更好时,却出现了11个错误:

Ra2.java:3: error: unclosed string literal
        System.out.println("   /\");
                           ^
Ra2.java:3: error: ';' expected
        System.out.println("   /\");
                                     ^
Ra2.java:4: error: illegal start of expression
        System.out.print("  /");
              ^
Ra2.java:4: error: ';' expected
        System.out.print("  /");
                  ^
Ra2.java:6: error: unclosed string literal
        System.out.println(" \");
                           ^
Ra2.java:6: error: ';' expected
        System.out.println(" \");
                                 ^
Ra2.java:7: error: illegal start of expression
        System.out.print(" /");
              ^
Ra2.java:7: error: ';' expected
        System.out.print(" /");
                  ^
Ra2.java:11: error: unclosed string literal
        System.out.print(" \");
                         ^
Ra2.java:11: error: ';' expected
        System.out.print(" \");
                               ^
Ra2.java:13: error: reached end of file while parsing
}
 ^
11 errors

1 个答案:

答案 0 :(得分:1)

这是因为(根据docs):

  

以反斜杠(\)开头的字符是转义序列,对编译器具有特殊含义

因此\转义了结尾的引号,导致未封闭的字符串。您需要这样做:

System.out.println(" /\\");