我試圖讓我的輸出顯示縮寫(xiě)周?chē)碾p引號(hào)以及翻譯后的縮寫(xiě)。但是我在當(dāng)前課程中沒(méi)有介紹轉(zhuǎn)義序列,所以我想知道是否有另一種方法可以實(shí)現(xiàn)這一點(diǎn)。當(dāng)我嘗試使用轉(zhuǎn)義序列時(shí),工作簿將不接受。我嘗試過(guò)轉(zhuǎn)義序列并使用兩個(gè)單引號(hào) ('' ''),但都沒(méi)有用。也許我遺漏了一些東西并且對(duì) Java 語(yǔ)言還很陌生。只是試圖從基礎(chǔ)的角度學(xué)習(xí)最有效的方法。導(dǎo)入 java.util.Scanner;公共類 TextMsgExpander { public static void main(String[] args) {Scanner scnr = new Scanner(System.in);String txtMsg;String BFF = "best friend forever";String IDK = "I don't know";String JK = "just kidding";String TMI = "too much information";String TTYL = "talk to you later";System.out.println("Enter text: ");txtMsg = scnr.nextLine();System.out.println("You entered: " + txtMsg);System.out.println();if(txtMsg.contains("BFF")) { txtMsg = txtMsg.replace("BFF", BFF); System.out.println("Replaced BFF with " + BFF);} // above line is where I tried escape sequenceif(txtMsg.contains("IDK")) { txtMsg = txtMsg.replace("IDK", IDK); System.out.println("Replaced IDK with " + IDK);}if(txtMsg.contains("JK")) { txtMsg = txtMsg.replace("JK", JK); System.out.println("Replaced JK with " + JK);}System.out.println();System.out.println("Expanded: " + txtMsg);return;} }你的輸出輸入文本: 您輸入了: IDK 這是怎么回事。下次再談。將 IDK 替換為 I don't know 將 TTYL 替換為 talk to you later擴(kuò)展:我不知道那是怎么發(fā)生的。我們回聊。預(yù)期產(chǎn)出輸入文本: 您輸入了: IDK 這是怎么回事。下次再談。將“IDK”替換為“我不知道”。將“TTYL”替換為“稍后與您交談”。擴(kuò)展:我不知道那是怎么發(fā)生的。我們回聊。
3 回答

當(dāng)年話下
TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
你有沒(méi)有試過(guò)這個(gè):
\"example text\"
所以你會(huì)有這樣的事情:
System.out.println("Replaced \"BFF\" with " + "\"" + BFF + "\"");
或者
System.out.println("Replaced \"BFF\" with \"" + BFF + "\"");

ITMISS
TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
通常它應(yīng)該與轉(zhuǎn)義字符一起使用。你有沒(méi)有試過(guò)這樣的事情:
System.out.println("\"These two semi colons are removed when i am printed\"");
我測(cè)試了它,它對(duì)我有用。

慕標(biāo)琳琳
TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果您不能使用\
轉(zhuǎn)義序列,無(wú)論出于何種原因,您都可以使用這樣一個(gè)事實(shí),即'
撇號(hào)不需要在"xx"
字符串文字中轉(zhuǎn)義,"
雙引號(hào)不需要在'x'
字符文字中轉(zhuǎn)義。
例如,要打印Replacing "foo" with 'bar' was easy
, 和foo
是bar
變量,您可以這樣做:
String s = "Replacing " + '"' + foo + '"' + " with '" + bar + "' was easy"`;
添加回答
舉報(bào)
0/150
提交
取消