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));
精彩评论