简体   繁体   中英

Java Program does not terminate

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

"return" within the TimerTask.run method returns from the TimerTask.run method. It does not make the getResponse() method exit.

To exit from getResponse() , the TimerTask has to set some kind of a flag, which is read by getResponse() . One possibility is to use an AtomicBoolean object. It's initially set to false , and when the timer triggers, it's changed to true :

    AtomicBoolean timeIsUp = new AtomicBoolean(false);
    Timer t = new Timer();
    t.schedule(new TimerTask() {
        public void run() {
            System.out.print("\n Time is up!");
            timeIsUp.set(true);
            t.cancel();
        }
    }, 5 * 1000L);

The loop in getResponse needs to check if the time is up:

    for (int i = 0; i < 10 && !timeIsUp.get(); i++) {
        ...
    }

However, this will only check for timeout between questions. While the program is waiting for the user to type, it cannot be interrupted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM