【问题标题】:Java string array double quote errorJava字符串数组双引号错误
【发布时间】:2013-06-23 18:17:40
【问题描述】:

当我写出我的字符串时,它告诉我字符串文字没有正确关闭,但看起来它是..?然后如果我取出 /|\ 错误会向下移动 2 行到腿。我研究了一下,我似乎无法知道问题是什么......

public static void printMan(int badGuesses) {
    String[] man = new String[];
             man={"______",
                  "|    |",
                  "|    o",
                  "|    |",
                  "|   /|\", //it tells me that i need to insert missing quote
                  "|    |",
                  "|   / \"
                 };
    int counter = 0;
    while (counter < badGuesses) {
        System.out.println(man[counter]);
    }

【问题讨论】:

  • 头部应该是大写O

标签: java


【解决方案1】:

看起来你有一个转义字符

\escape character,在这种情况下你也需要转义它。否则,你会得到一个未终止的字符串。 \" 表示实际字符",而不是字符串的开头或结尾。

如果您想要实际字符 \,您也需要转义它:\\

 String[] man = new String[]{
              "|    |",
              "|    o",
              "|    |",
              "|   /|\\",  \\<- note the extra \
              "|    |",
              "|   / \\"   \\<- note the extra \ here too
             };

官方Java教程

参见官方java教程中的section on escape sequences

以反斜杠 (\) 开头的字符是转义序列,对编译器具有特殊意义。下表显示了 Java 转义序列(我:链接中的表)

语言规范

这当然符合language specification

StringLiteral:
   "StringCharacters"

  StringCharacters:
   StringCharacter
   | StringCharacters StringCharacter

  StringCharacter:
   InputCharacter but not " or \
   | EscapeSequence

   
EscapeSequence:
    \ b    
    \ t   
    \ n   
    \ f   
    \ r    
    \ "   
    \ '  
    \ \  < -  **THIS IS THE ONE YOU HAD**
    OctalEscape   /* \u0000 to \u00ff: from octal value */

(related question)

更广阔的视野

维基百科在转义字符上也有一个interesting article

在计算和电信中,转义字符是对字符序列中的后续字符调用替代解释的字符。转义字符是元字符的一种特殊情况。一般来说,判断某事物是否为转义字符取决于上下文。

注意:

C、C++、Java 和 Ruby 都允许完全相同的两种反斜杠转义样式。

这里是 another related question here on SO 关于转义字符串。

【讨论】:

  • 现在是了解社区维基的好机会。为什么这个答案是一个? :D
  • @MarounMaroun 避免获得 OP 认为不应获得的代表
  • @MarounMaroun 这是一个微不足道的问题,因此我不想代表它。另一方面,这是很多用户都知道的事情,所以它很快就得到了很多不同用户的大量编辑——这样做让我们能够快速生成高质量的答案。
【解决方案2】:

这是因为您使用了 excape 字符。\" 导致考虑 ",添加 \\"

man={"______",
          "|    |",
          "|    o",
          "|    |",
          "|   /|\\",  \\ and extra slash here
          "|    |",
          "|   / \\"   \\ and here
         };

【讨论】:

    【解决方案3】:

    您还声明了错误的数组,如果您使用初始化器必须以这种方式,否则您必须为数组提供大小。

    String[] man = new String[]{"______",
                 "|    |",
                 "|    o",
                 "|    |",
                 "|   /|\\",
                 "|    |",
                 "|   / \\"
                };
    

    【讨论】:

      【解决方案4】:

      \ 是转义字符。您必须再次对其进行转义,因此最后一行应如下所示:

      "|   / \\"
      

      【讨论】:

        【解决方案5】:

        public static void printMan(int badGuesses) {

        String[] man = new String[];
                 man={"______",
                      "|    |",
                      "|    o",
                      "|    |",
                      "|   /|\", //it tells me that i need to insert missing quote
                      "|    |",
                      "|   / \"
                     };
        int counter = 0;
        while (counter < badGuesses) {
            System.out.println(man[counter]);
        }
        

        建议: 1. 在你在代码中使用的每个 \ 之前添加一个额外的 \。

        1. 匿名初始化数组时,在声明中进行初始化是一种很好的做法。像 字符串 []man=new String[]{"_","| |"};

        【讨论】:

          猜你喜欢
          • 2012-09-27
          • 2018-01-03
          • 1970-01-01
          • 2014-09-18
          • 1970-01-01
          • 2012-09-03
          • 2014-09-26
          • 2014-09-20
          • 2016-10-29
          相关资源
          最近更新 更多