repeat a particual function in java code example
Example 1: java repeat loop cycle for
for(int i = 0; i < 10; i++) {
//your code
if(random.nextInt(5) == 0) { i--; } //1 in 5 chance to repeat loop cycle
}
Example 2: how to write a method that returns a string that copies itself times n
public static String repeat(String s, int n) { StringBuilder sb = new StringBuilder(str.length() * n); for (int i = 0; i < n; i++) sb.append(s); return sb.toString();} public static void main(String[] args) { System.out.println(repeat("ha", 5));}