1

我想打印这个标志/\,但在编译器上,我认为一切都很好,但无法编译。错误显示为

leets.java:13: error: unclosed string literal
System.out.printf ("%s /\",ch);
                     ^
 leets.java:13: error: ';' expected
 System.out.printf ("%s /\",ch);
                     ^
 2 errors

我的代码如下。

import java.util.Scanner;
public class switchDemo2
{
    public static void main ( String args[] )
    {
    Scanner i = new Scanner ( System.in );
    System.out.print ( "Enter a character to test: " );
    char ch;
    ch = i.next().charAt(0);
    switch ( ch )
    {
    case 'A': case 'a':
    System.out.printf ("%s /\",ch);
                       break;
    case 'B': case 'b':
        System.out.printf ("%s 13",ch);
    break;
    case 'C': case 'c':
    System.out.printf ("%s )",ch);
    break;
    case 'D': case 'd':
    System.out.printf ("%s 1)",ch);
    break;
    case 'E': case 'e':
    System.out.printf ("%s 3",ch);
    break;
    case 'F': case 'f':
    System.out.printf ("%s 1=",ch);
    break;
    default:
    System.out.printf ("%s not a lowercase vowel\n",ch);
}
}
4

4 回答 4

4

您需要将反斜杠转义为 .

 System.out.printf ("%c /\\",ch);

您可以在此处查看有关它的更多信息。

几条评论:

  1. 你用于"%s"字符,你可以"%c用于字符。请参阅格式化教程
  2. 你没有关闭扫描仪,你会得到这样的警告。您应该使用Scanner.close()方法关闭它。
  3. }剩下最后一个花括号。
于 2012-10-05T06:02:37.383 回答
1
System.out.printf ("%s /\\",ch);

代替System.out.printf ("%s /\",ch);

 Escape Sequence    Description
 \t     Insert a tab in the text at this point.
 \b     Insert a backspace in the text at this point.
 \n     Insert a newline in the text at this point.
 \r     Insert a carriage return in the text at this point.
 \f     Insert a formfeed in the text at this point.
 \'     Insert a single quote character in the text at this point.
 \"     Insert a double quote character in the text at this point.
 \\     Insert a backslash character in the text at this point

这是关于java中字符的教程

于 2012-10-05T06:03:07.377 回答
0

需要转义反斜杠:-

System.out.printf ("%c /\\",ch);

您可以将: - 更改switch ( ch )switch(Character.toLowerCase(ch)), 以避免同时出现A和的情况a。只需使用case 'a':

于 2012-10-05T06:03:35.030 回答
0

这应该适合你;

System.out.printf("%s '/\\'",ch);

问候,

迪努卡

于 2012-10-05T06:07:31.263 回答