条件逻辑和字符串操作辅助

时间:2017-03-23 22:36:43

标签: 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.

这是我得到的输出

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);
    }
}

1 个答案:

答案 0 :(得分:1)

std::vector<std::vector<std::string>> vectors(3); while(std::getline(inData, line)){ std::istringstream iss{line}; std::string token; size_t i = 0; while(iss >> token){ vectors[i++].push_back(token); } } 应为userMSG.contains(BFF)。您需要对其他条件应用相同的更改。

请注意,使用地图可能会更容易(如代码较少)。