2012-10-05 57 views
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); 
} 
} 
+0

你需要閱讀有關使用Java – jondinham

+0

文本字符串轉義序列,正如其他人告訴你,那就是缺少一個轉義序列,它不是一個很好的做法來命名從較小的案例開始。將它重命名爲SwitchDemo2 :) – DDK

回答

4

您需要將反斜槓轉義爲。

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

您可以查看關於它的更多信息here

幾點意見:

  1. 你以前"%s"的字符,你可以使用"%c的字符。請參閱格式tutorial
  2. 您沒有關閉掃描儀,您會收到警告。您應該使用Scanner.close()方法關閉它。
  3. 最後的大括號}剩下的。需要
+2

+1僅僅是因爲速度很快(你看,他們都說相同,我也沒有意義,所以我只會讓你高興)。 – Theraot

+0

+1。你應該使用'System.out.printf(「%c/\\」,ch);'因爲ch是char(不是String)。 – Axel

+0

謝謝,很有幫助。我正在嘗試寫一個字母表。我的程序會將信件轉換爲leet。我應該將所有信件作爲案例寫入還是有其他方式? –

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 

Here is the Tutorial on characters in java

0

反斜線進行轉義: -

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

,你可以改變你的: - switch (ch)switch(Character.toLowerCase(ch)),以避免同時出現Aa的情況。只要使用case 'a':

0

這應該適合你;

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

問候,

Dinuka