例如用户类型:水果苹果是我的最爱.我想回复:水果*****是我最喜欢的.
但我的代码只返回第一个单词.
public static void main(String[] args) {
System.out.print("Type someting: ");
Scanner scan = new Scanner(System.in);
String s = scan.next();
String [] myArray = {"apple", "banana", "strawberry"};
boolean matchFound = false;
for (int i = 0; i < myArray.length; i++) {
if (s.toLowerCase().contains(myArray[i].toLowerCase())){
String beep = String.join("", Collections.nCopies(myArray[i].length(), "*"));
String result = s.replaceAll(myArray[i].toLowerCase(), beep);
System.out.println(result);
matchFound = true;
}
}
if (matchFound == false){
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud)