0

Increase an int and then reset it and repeat.

I want to have an int = 0, then increase until it reaches 4 and then reset it to 1 and then repeat this. So it goes 1 2 3 4 1 2 3 4 indefinitely.

1st Apr 2019, 12:44 PM
Alex
Alex - avatar
2 Respuestas
0
public class Program { public static void main(String[] args) { int i = 0; while (i < 4) { System.out.print(++i); if (i == 4) i = 0; } } }
1st Apr 2019, 12:56 PM
Hasan Oktay
0
Use Hasan Oktay code, but i would make it sleep a second or two when you raising 4 by add Thread.sleep(2000) in your if statement. But when using Thread.sleep it may throw an exception so use throws Exception after your main: public static void main(String[] args) throws Exception Or use try-catch block.
1st Apr 2019, 4:10 PM
JavaBobbo
JavaBobbo - avatar