【问题标题】:Java Program does not terminateJava 程序不终止
【发布时间】:2020-08-21 03:58:53
【问题描述】:

我的程序在计时器启动后继续运行...我希望在计时器启动后不再进行输入并转到 showResult() 函数...但是程序在显示结果后跳回接受输入。

如果计时器没有启动,程序就很好了……但是当计时器打开时……我认为一个线程仍然停留在我接受输入的函数上,程序返回的flw当标记已显示时。我不希望发生这样的行为。一旦计时器启动,程序应该停止接收输入,并且永远不会再回到它。我不知道该怎么做。 我是这个领域的菜鸟: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);
  }
}

任何建议将不胜感激:D

【问题讨论】:

  • 您的程序可能正在等待输入,但我怀疑您的程序是否完全正常,因为它几乎立即抛出 ArrayIndexOutOfBoundsException
  • 问题是 Scanner(System.in) 在单独的线程中等待输入。我什至不确定这可以像这样在命令行上完成。您可以尝试将输入放在不同的线程中,然后强制终止输入线程,但这并不漂亮。
  • 您可能需要考虑在主线程上保留计时器。根据程序的终止方式,将有助于概念化这一点。 1) 计时器 2) 验证正确答案后
  • @JacoVanNiekerk 你能告诉我可以使用哪些不同的输入线程以及如何终止它吗?
  • @shinjw 你能告诉我怎么做吗......你已经明白我到底想要什么,但问题是可能有各种各样的问题,我不希望程序在一个一个问题。

标签: java timer terminate


【解决方案1】:

TimerTask.run 方法中的“return”从TimerTask.run 方法返回。它不会使getResponse() 方法退出。

要退出getResponse(),TimerTask 必须设置某种标志,由getResponse() 读取。一种可能性是使用AtomicBoolean 对象。最初设置为false,当定时器触发时,改为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);

getResponse中的循环需要检查时间是否到了:

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

但是,这只会检查问题之间的超时。当程序在等待用户输入时,它不能被中断。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
相关资源
最近更新 更多