2017-03-23 2 views
-3

내가 잘못하고 있는지 확실하지 않습니다. 어떤 도움이라도 좋을 것입니다!조건부 논리 및 문자열 연산 지원

Enter text: IDK how that happened. TTYL. 
You entered: IDK how that happened. TTYL. 

Replaced "IDK" with "I don't know". 
Replaced "TTYL" with "talk to you later". 

Expanded: I don't know how that happened. talk to you later. 

이 내가을해야

debug: 
Enter text: IDK how that happened. TTYL. 
You entered: IDK how that happened. TTYL. 
BUILD SUCCESSFUL (total time: 30 seconds) 
package textmsgexpander; 

import java.util.Scanner; 

/** 
* 
*/ 
public class TextMsgExpander { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     Scanner scnr = new Scanner(System.in); 
     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"; 
     String userMSG = ""; 

     //User enters the text and gets output 
     System.out.println("Enter text: "); 
     userMSG = scnr.nextLine(); 

     //Program outputs what user entered above 
     System.out.println("You entered: " + userMSG); 

     if (userMSG.contains(BFF)){ 
      userMSG = userMSG.replace("BFF", BFF); 
      System.out.println("Replaced 'BFF' with " + BFF); 
     { 
     else if (userMSG.contains(IDK)) { 
      userMSG = userMSG.replace("IDK", IDK); 
      System.out.println("Replaced 'IDK' with " + IDK);  
     } 
     else if (userMSG.contains(JK)); { 
      userMSG = userMSG.replace("JK", JK); 
      System.out.println("Replaced 'JK' with " + JK);  
     } 
     else if (userMSG.contains(TMI)); { 
      userMSG = userMSG.replace("TMI", TMI); 
      System.out.println("Replaced 'TMI' with " + TMI);  
     } 
     else if (userMSG.contains(TTYL)); { 
      userMSG = userMSG.replace("TTYL", TTYL); 
      System.out.println("Replaced 'TTYL' with " + TTYL); 
     else { 
      System.out.println("Unknown"); 
     } 

     //Program outputs message with expanded abbreviations 
     System.out.println("Expanded: " + userMSG); 
    } 
} 
+0

두 개 이상의 약어를 대체해야하는 경우 왜 'else if'를 사용합니까? – fabian

답변

1

userMSG.contains(BFF) 받고 있어요 출력은 다음과 같습니다


내가 찾고 있어요 출력입니다. 다른 조건에도 동일한 변경 사항을 적용해야합니다.

지도를 사용하면 코드가 적어 질수록 쉬울 것입니다.

+0

답장을 보내 주셔서 감사합니다. 나는 완전한 새다. 지도에서 무엇을 의미합니까? – User123456789

+0

https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html. – assylias

+1

감사합니다. 나는 그것을 사용하는 것을 허락하지 않는다. 조건부 논리 및 문자열 연산. – User123456789