我有一個問題。我正在努力進行一項練習,在練習中,我被要求從用戶那裏獲得一個字符串,一個我想在此字符串中複製的字符以及一個數字 - 我要複製多少次。例如:字符串輸入:狗;字符:o;數量:4。輸出:doooog。我的問題是我怎樣才能達到這個結果?重複字符串java中的特定字符
Scanner sc = new Scanner(System.in);
System.out.println("enter your string");
String text = sc.nextLine();
System.out.println("enter the character that will be repeated");
char character= sc.next().charAt(0);
System.out.println("enter the number of repetitions");
int repetitions = sc.nextInt();
for (int i = 0; i < text.length(); i++) {
char z = text.charAt(i);
if(z==character) {
// repeat character in string put by user * repetitions
}
}
System.out.println(text);