【发布时间】:2017-08-13 13:30:56
【问题描述】:
我有一个问题。我正在努力进行一项练习,要求我从用户那里获取一个字符串、一个我想在这个字符串中复制的字符和一个数字——我想复制多少次。 eg:字符串输入:dog;字符:o;数量:4。输出:doooog。我的问题是我怎样才能达到这个结果?
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);
【问题讨论】:
标签: java string character repeat