您只需要为字符串中的每个字符添加n次字符串本身。您需要遍历字符串,并将每个字符追加n次。
public static void repeatLetters(){ String text = "dean"; int n = 3; StringBuilder repeat = new StringBuilder(); for (int i = 0; i < text.length(); i++) { for (int j = 0; j < n; j++) { repeat.append(text.charAt(i)); } } System.out.println(repeat);}另外,另一种解决方案是使用正则表达式。
public static void repeatLetters(){ String text = "dean", replace = ""; int n = 3; for (int i = 0; i < n; i++) replace += "$1"; System.out.println(text.replaceAll("(.)", replace));}


