2017-01-22 19 views
0

私はまったく新しいものです。私はこれを行う方法を私に説明できるチュートリアルまたはリソースを探しています:JAVAの条件付き論理演算と文字列演算

拡張された各省略語に対してメッセージを出力し、拡張された行を出力します。

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. 

あなたが見つけることString.indexOf()を使用することができ、私はI don't knowIDKを変更するuserText.replace一部を行う方法を知っているが、私はIDK

+0

なぜあなた自身を検索する必要がありますか? 'replace'はすでにあなたのために検索と置換を行います.... – Li357

+0

私はHashMap を見てみることをお勧めします:https://docs.oracle.com/javase/8/docs/api/java/util/ HashMap.html –

+0

私が言ったように、私は何をしているのか分かりませんが、私は条件付き書式設定とidkを使う方法を知っています。 –

答えて

0

の文字列を検索するためにそれを設定する方法がわかりません文字列の最初のインスタンス:

String enteredText = "IDK how that happened. TTYL."; 
int pos = enteredText.indexOf("IDK"); // pos now contains 0 
pos = enteredText.indexOf("TTYL"); // pos now contains 23 

文字列が見つからない場合は-1を返します。

あなたは価値があなたのメッセージをあなたの交換や出力を行う、そのpos != -1)をテストすることによって、(発見されたことを知っていたら。

0

使用String.indexOf()各略語は、文字列を変更するために、入力文字列とreplaceAll()に存在するかどうかをチェックしますそれがない場合:

import java.util.Scanner; 

class Main { 
    public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 
    System.out.print("Enter text: "); 
    String text = scanner.nextLine(); 
    System.out.println("You entered: " + text); 
    if(text.indexOf("IDK") != -1) { 
     System.out.println("Replaced \"IDK\" with \"I don't know\""); 
     text = text.replaceAll("IDK", "I don't know"); 
    } 
    if(text.indexOf("TTYL") != -1) { 
     System.out.println("Replaced \"TTYL\" with \"talk to you later\""); 
     text = text.replaceAll("TTYL", "talk to you later"); 
    } 
    System.out.println("Expanded: " + text); 
    } 
} 

出力:

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. 

here!

注試してみよう:実装上のこれは、私はあなたがtoLowerCase()にまたはtoUppperCase()に見えることを示唆している問題のための入力のいずれかの不規則な総額を扱っていません。