2017-03-23 6 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

ありがとうございます。私は完全なnewbです。地図ではどういう意味ですか? – User123456789

+0

これはhttps://docs.oracle.com/javase/tutorial/collections/interfaces/map.htmlです。 – assylias

+1

ありがとうございます。私はそれを使用することはできません。条件付きロジックと文字列操作。 – User123456789

関連する問題