Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的扫描仪/终端窗口在重复一次后停止工作_Java_Timer_Java.util.scanner_Gridworld - Fatal编程技术网

Java 我的扫描仪/终端窗口在重复一次后停止工作

Java 我的扫描仪/终端窗口在重复一次后停止工作,java,timer,java.util.scanner,gridworld,Java,Timer,Java.util.scanner,Gridworld,我目前正在gridworld上进行一个塔防项目(不确定这是否重要)。我有一个计时器,它从我的主类调用一个方法(TowerPlacer),通过扫描仪接收用户文本输入。但是,在第一次通过后,终端窗口停止允许我输入文本,尽管TowerPlacer计时器正在运行 此外,无论出于何种原因,该方法似乎也能阻止所有bug的产生。尽管这座塔实际上什么也没做 下面是TowerPlacer的方法 public static void TowerPlacer () { System.out.println ("Y

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

此外,无论出于何种原因,该方法似乎也能阻止所有bug的产生。尽管这座塔实际上什么也没做

下面是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();
    }
  }
}

错误在你的计时器任务中。您需要的是:

class SayHello extends TimerTask {

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

 }
然后将主要方法更改如下:

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);
}

如果
计时器已经调用了
TowerPlacer()
,为什么你自己在main中调用它呢?游戏开始时,我在main中调用了TowerPlacer(),以便玩家可以立即放置塔,然后计时器继续调用它。嗨!谢谢你的快速回复!这修复了产生的bug,但是终端窗口现在根本不接收任何输入(与以前相反,它在锁定之前只接受一次输入)。对不起,回复太晚了,昨天在舞会上很忙哈哈。我在gridworld外分别用定时器和kbReader做了一个测试,但我仍然遇到无法在终端中输入扫描仪的问题。别管!我用一个非常奇怪的while循环解决了终端窗口问题。无论我做了什么,终端窗口都不允许我在计时器运行时输入,但我通过一个几乎无限输出的while循环来解锁窗口。。。看不出背后的逻辑,但它是有效的!不过我很感激你们的回答,它解决了我在繁殖塔楼时虫子不繁殖的问题!