在人 - CPU游戏期间安排两个计时器任务时,多个计时器将运行

问题描述 投票:0回答:1

再次。当我试图在一个回合后尝试任务时,我一直得到一个IllegalStateException。具体而言,提到任务已经安排或取消。你知道怎么解决这个问题吗?这是一些相关的代码。

public class Human_Agent implements GipfPlayable {

    String[] edgeSpots;
    String[] columns;
    Random rng;
    GipfGame mg;
    //int turn = 0;
    String str;
    Timer t;
    Integer val;
    Integer myDir;
    public Human_Agent(GipfGame g) {
        this.edgeSpots = g.getEdgeSpots();
        this.columns = g.getColumns();
        rng = new Random(37);
        //turn = 0;
    }
    TimerTask task1 = new TimerTask()
    {
        public void run()
        {

            if (str.equals(""))
            {
                System.out.println("You did not input a valid location.");
                System.exit(0);
            }

        }
    };
    TimerTask task2 = new TimerTask()
    {
        public void run()
        {

            if (val == null)
            {
                System.out.println("You did not input a valid direction.");
                System.exit(0);
            }
        }
    };

    public String getUserInput() throws Exception
    {
        t = new Timer();
        t.schedule(task1, 5*1000);
        Scanner scan = new Scanner(System.in);
        System.out.print("Please provide a starting location: ");
        String startLocation = "";
        String x = scan.nextLine();
        str = x;
        int dirE = 0;
        //t.cancel();
       // Timer t2 = new Timer();
        if (isItValidLocation(x))
        {
        startLocation = x;
        t.cancel();
        t = new Timer();
        t.schedule(task2, 10*1000);
        //t.scheduleAtFixedRate(task2, 5000, 5*1000);
        System.out.print("\n Now, provide a number to move the piece");
         dirE = scan.nextInt();
         t.cancel();
        }
        else
        {
            System.out.println("Invalid Start location.");
            t.cancel();
        }
       // scan.close();
       // t.cancel();
         return str + " " + Integer.toString(dirE);
    }
    @Override
    public String makeGipfMove() {
        String s;
        try
        {
            s = getUserInput();
        }
        catch(Exception e)
        {
        String startLocation = edgeSpots[rng.nextInt(edgeSpots.length)];
        Integer dir = rng.nextInt(6);
        s = startLocation + " " + Integer.toString(dir);
        e.printStackTrace();
        }

//        if ((mg.makeMove(x, dirE)) == false)
//        {
//            System.out.println("Can't move here.");
//        }
        //mg.makeMove(x, dirE);
       // Integer dir = rng.nextInt(6);
        //System.out.println("GIPHS REMAINING: " + )

       return s;
    }
    public boolean isItValidLocation(String a)
    {
        boolean v = false;
        System.out.println("YOU ENTERED: " + a);
        for (int i = 0; i < edgeSpots.length; i++)
        {
            System.out.println(edgeSpots[i]);
            if ((edgeSpots[i].equals(a)) && (v != true))
            {
                 v = true;
            }

        }
        System.out.println(v);
        return v;
    }

}

这段代码的作用是用人工代理玩Gipf游戏。

看起来你的帖子主要是代码;请添加更多细节。 (如果您添加了有关代码的详细信息,Netbeans需要摆脱这个!)

java timer illegalstateexception timertask
1个回答
0
投票

同样的方法,你取消它后不能重复使用Timer,你不能重复使用TimerTask

您可以创建扩展TimerTask的类,并在需要安排时实例化它们。

© www.soinside.com 2019 - 2024. All rights reserved.