How I can stoped or canceled my timertask void ? Because TimerTask void stopped my loop and I don't know how I can resolve my problem.
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
public class Main {
public static void main(String [] args) throws Exception {
int hp = 10;
Scanner sc = new Scanner(System.in);
do{
Random los = new Random();
int pc = 1 + los.nextInt(6);
TimerTask zd = new TimerTask(){
public void run(){
System.out.println("Czas off");
System.out.println("5 sek");
}
};
Timer czas = new Timer();
czas.schedule(zd, 5 * 1000);
System.out.println("5 sek na odp");
String odp = sc.nextLine();
switch(odp){
case "atk":
System.out.println("oo tak");
hp = hp - pc;
System.out.println("hp spadlo o " + pc + " " + " wynosi " + hp);
break;
default:
System.out.println("zle");
hp = hp + pc;
System.out.println("hp wzroslo o " + pc);
}
}while(hp>0);
System.out.println("pokonany");
}
}
There is a code, when time out. My loop has stop working and them is starter a void run, but I cant go back to my loop. How I can resolve this?
I'm try using zd.cancel(); But this doesnt work. And I'm make another methods without i came back to main methods again, then I have a bugs with program.