我尝试了以下内容,但无法输出任何内容。理想情况下,我希望通过int multiplier让String文本重复n次。我做错了什么?
String text和int multiplier作为另一个方法的参数传递。
public static String repeatText(String text, int multiplier)
{
String value = "";
StringBuilder repeat = new StringBuilder(text.length() * multiplier);
repeat(repeat, text, multiplier);
value.equals(repeat);
System.out.println("Text Repeated:");
System.out.println("-----------");
System.out.println(repeat);
System.out.println("--------------");
return value;
}
答案 0 :(得分:1)
尝试以下方法:
public static String repeat(String toRepeat, int times) {
if (toRepeat == null) {
toRepeat = "null";
}
if (times <= 0) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++) {
sb.append(toRepeat);
}
return sb.toString();
}
答案 1 :(得分:1)
用户循环
for(int i=1; i<=multiplier; i++)
{
your logic
}
答案 2 :(得分:0)
public static void repeat(StringBuilder repeat,String text, int multiplier){
for(int i=0; i<multiplier; i++){
repeat.append(text);
}
}