I have a timer class that prints out a string every 5 ticks, and on the 6th tick it runs a random event. now what i want is to have the timer to restart from 9 ticks. As i tried to do with the else statement in the MyTask Class withing the TimerSample class below.
And one more question: How can i implement an if statement in the the main class so that i can have the user to enter a value inorder to run the timer?
Thanks in advance!!
The Class
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Scanner;
public class TimerSample {
public static void main(String[] args) {
Timer timer = new Timer("Printer");
MyTask t = new MyTask();
timer.schedule(t, 0, 2000);
}
}
class MyTask extends TimerTask {
private int times = 0;
public void run() {
times++;
if (times <= 5) {
System.out.println("Time is Passing...");
}else if(times > 5) {
Random rand = new Random();
RandomEvents newREvent = new RandomEvents(rand.nextInt(30));
}else if(times == 7){
times = 0;
}
}
}