开发者

how to just print the statement without any loop condition in java?

I just want to print the "hello" 5 times without using for lo开发者_运维技巧op,while loop ?


System.out.println("hello hello hello hello hello");


You can do this with recursion. Is this homework? Sounds like it, so I'll not give a full answer unless you say otherwise.


public String repeat(String str, int times){
   return new String(new char[times]).replace("\0", str);
}
//...
String bob = "bob ";
System.out.println(repeat(bob, 5));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜