JAVA中的条件逻辑和字符串操作

JAVA中的条件逻辑和字符串操作,java,conditional,Java,Conditional,我是全新的,完全迷路了。我正在寻找可以向我解释如何做到这一点的教程或资源: 为展开的每个缩写输出消息,然后输出展开的行 e、 g 我知道如何使用userText.replace部分将IDK更改为我不知道,但我不知道如何设置它来搜索字符串中的IDK您可以使用string.indexOf()来查找给定字符串的第一个实例: String enteredText = "IDK how that happened. TTYL."; int pos = enteredText.indexOf("IDK");

我是全新的,完全迷路了。我正在寻找可以向我解释如何做到这一点的教程或资源:

为展开的每个缩写输出消息,然后输出展开的行

e、 g


我知道如何使用
userText.replace
部分将
IDK
更改为
我不知道
,但我不知道如何设置它来搜索字符串中的
IDK
您可以使用
string.indexOf()
来查找给定字符串的第一个实例:

String enteredText = "IDK how that happened. TTYL.";
int pos = enteredText.indexOf("IDK");    // pos now contains 0
pos = enteredText.indexOf("TTYL");    // pos now contains 23
如果
indexOf()
找不到字符串,则返回-1

一旦知道找到了值(通过测试
pos!=-1)
,执行替换并输出消息。

使用检查查看输入字符串中是否存在每个缩写,如果存在,则修改字符串:

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.
试试看


注意:上述实现不处理该问题输入的任何不规则大写。我建议您调查或查看。

您为什么需要自己搜索
replace
已经为您搜索和替换了……我建议您查看HashMap:不确定,正如我所说,不知道我在做什么,但我知道我应该使用条件格式和idk如何实现这一点我认为我们还没有涵盖Alexey,但我会在那里查看
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.