My program keeps running after the timer kicks in... I want no more input to be taken after timer kicks in and go to the showResult() function... But program jumps back to taking input after displaying the results.
The program is perfectly fine if the timer doesn't kick in... But when the timer is on... I think a thread is still stuck at the function where i take the input and the flw the program goes back there when marks have been displayed. I don't want such a behavior to occur. The program should stop taking input as soon as the timer kicks in and never go back to it again. I am unable to figure out how to do so. I am a rookie in this field :P
import java.util.*;
class Quiz {
private String questions[][];
private int marks;
public Quiz(){
marks = 0;
questions = new String[][]{{
"Name of the screen that recognizes touch input is :",
"Recog screen",
"Point Screen",
"Android Screen",
"Touch Screen",
"4",
""
}};
//questions as stated above is filled.
}
public void displayQues(int x){
System.out.print("\n Q. "+questions[x][0]);
System.out.print("\n 1. "+questions[x][1]);
System.out.print("\n 2. "+questions[x][2]);
System.out.print("\n 3. "+questions[x][3]);
System.out.print("\n 4. "+questions[x][4]);
}
public void getResponse(){
Timer t = new Timer();
t.schedule(
new TimerTask(){
public void run(){
System.out.print("\n Time is up!");
t.cancel();
return;
}
}, 5*1000L);
System.out.print("\n Timer of 5 Minutes started!");
String temp = "";
for(int i = 0;i < 10;i++){
int x = genDiffRndNum(temp);
displayQues(x);
System.out.print("\n Enter your answer: ");
if(validateAnswer(x))
questions[x][6] = "T";
else
questions[x][6] = "F";
temp = temp+x;
}
}
public int genDiffRndNum(String str){
while(true){
int n = (int)(Math.random()*10);
if(str.indexOf(Integer.toString(n))==-1)
return n;
}
}
public boolean validateAnswer(int ques){
Scanner sc = new Scanner(System.in);
int ans = sc.nextInt();
sc.close();
if(Integer.parseInt(questions[ques][5])==ans){
marks += 3;
return true;
}
marks -= 1;
return false;
}
public void showResults(){
System.out.print("\n Marks Obtained: "+marks);
System.exit(0);
}
public static void main(String[] args) {
Quiz q = new Quiz();
q.getResponse();
q.showResults();
System.exit(0);
}
}
Any suggestions would be appreciated :D