import java.util.Scanner;
//新建一個類
public class Replace {
// 定義一個字串
String commons;
// 建一個替換的方法
public void replace() {
//控制臺輸入
Scanner sc = new Scanner(System.in);
// 提示輸出(為方便功能實作,此處提示內定的敏感詞匯)
System.out.println("請輸入一句話:(敏感詞匯:垃圾,投降,太坑了,掛機,舉報)");
// 接收用戶輸入的內容
commons = sc.nextLine();
// 將敏感詞匯添加到字串陣列
String[] words = { "垃圾", "投降", "太坑了", "掛機", "舉報" };
for (String str : words) {
// 將用戶輸入內容中的敏感詞匯用*替換
commons = commons.replace(str, placeholder(str));
}
System.out.println(commons);
}
// 創建方法 ,根據提供的字符個數回傳對應長度的*
public String placeholder(String s) {
String p = "";
for (int i = 0; i < s.length(); i++) {
p += "*";
}
return p;
}
public static void main(String[] args) {
Replace r = new Replace();
r.replace();
}
}
輸出結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/212539.html
標籤:其他
上一篇:路由器的子介面
下一篇:Java寫的第一個小游戲
