一次重复后,我的扫描仪/终端窗口停止运行

时间:2013-06-08 06:21:30

标签: java timer java.util.scanner gridworld

我目前正致力于gridworld上的塔防工程(不确定这是否重要)。我有一个计时器从我的主类调用方法(TowerPlacer)来通过扫描仪接收用户文本输入。但是,在第一次通过后,终端窗口停止,允许我输入文本,尽管TowerPlacer计时器正在运行。

此外,无论出于何种原因,该方法似乎也可以阻止产生的所有错误。尽管塔被催生,但实际上什么也没做。

这是TowerPlacer的方法

public static void TowerPlacer ()
{
  System.out.println ("You have " + money + " gold");
  Scanner kbReader = new Scanner(System.in);
  System.out.println ("Would you like to place a defense?");
  System.out.println ("1. Yes");
  System.out.println ("2. No");
  answer = kbReader.nextInt();
  kbReader.nextLine();
  if (answer == 1) {
    System.out.println ("Which defense would you like to place?");
    System.out.println ("1. Study defense - 50 gold");
    System.out.println ("2. Pencil defense - 100 gold");
    System.out.println ("3. Pen defense - 150 gold");
    System.out.println ("4. Cram defense - 200 gold");
    Tanswer = kbReader.nextInt();
    kbReader.nextLine();
    System.out.println ("Defenses cannot be placed in the path of the critters.");
    System.out.println ("Defenses not placed in viable locations will end the game.");
    System.out.println ("What is the X-coordinate of the defense? (0 - 9)");
    xCoord = kbReader.nextInt();
    kbReader.nextLine();
    System.out.println ("What is the Y-coordinate of the defense? (0 - 9)");
    yCoord = kbReader.nextInt();
    kbReader.nextLine();
    if (Tanswer == 1) {
      Study defense1 = new Study();
      world.add(new Location(xCoord, yCoord), defense1);
      System.out.println ("Defense Placed");
    }
    if (Tanswer == 2) {
      Study defense2 = new Study();
      world.add(new Location(xCoord, yCoord), defense2);
      System.out.println ("Defense Placed");
    }
    if (Tanswer == 3) {
      Study defense3 = new Study();
      world.add(new Location(xCoord, yCoord), defense3);
      System.out.println ("Defense Placed");
    }
    if (Tanswer == 4) {
      Study defense4 = new Study();
      world.add(new Location(xCoord, yCoord), defense4);
      System.out.println ("Defense Placed");
    }
  }
  if (answer == 2) {
    System.out.println ("Construction cancelled");
  }
  if (answer != 1 && answer != 2) {
    System.out.println ("Error, answer out of bounds");
  }
  System.gc();
}

以下是我的计时器来电者的代码

public static void main(String[] args)
{
  Timer timer = new Timer();
  TimerA a = new TimerA();
  timer.schedule(a,0,2000);
  TimerC t = new TimerC();
  timer.schedule(t,0,2000);
  world.show();
  System.out.println ("Please do not pause the game after starting.");
  TowerPlacer();
}

计时器本身

class TimerA extends TimerTask
{
  public static int times = 0;
  public void run()
  {
    times++;
    if (times%10 == 0) {
      BoxBugRunner.TowerPlacer();
    }
    if (times >= 1000000) {
      //Stop Timer.
      this.cancel();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

错误在于你的TimerTask。你需要的是:

class SayHello extends TimerTask {

    public void run()
    {
      System.out.println("done");
    }

 }

然后按如下方式更改main方法:

public static void main(String[] args)
{
    int delay = 2000; // delay for 2 seconds
    int period = 500; // repeat every .5 seconds

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new SayHello(), delay, period);
}