【问题标题】:How can I store and add items back to a queue如何存储项目并将其添加回队列
【发布时间】:2021-10-12 15:52:12
【问题描述】:

我不知道如何存储第一个队列中的项目,保存它们然后重新添加它们

现在它通过一次然后队列是空的

我知道概念只是不知道语法

尝试创建第二个队列并在队列为空时重新添加玩家,但没有成功

        Queue<Baseball_PlayerItem> players = getplayers();
        int ct = 0;int outCt = 0;
        boolean game = true;
        for (Baseball_PlayerItem item : players) {
            while (game) {
                do {
                    Random rand = new Random();
                    double rand2 = rand.nextDouble();
                    item = players.remove();
                    if (item.getBatAvg() > rand2) {
                        System.out.printf("\nHit Perc: %s player: %s", rand2, item.getBatAvg());
                        ct++;
                        if (ct >= 3) {
                            System.out.printf("\nAnother home run total: %s", ct);
                        }
                    } else if (item.getBatAvg() < rand2) {
                        System.out.printf("\nThats Miss");
                        outCt++;
                        if (outCt == 3) {
                            System.out.printf("\nThree outs game over :( Outs: %s",outCt);


                        }else if (players.isEmpty()) {
                            System.out.printf("\nAll done");
                            checkGameStatus();
                            game = false;
                        }
                    }

                } while (game = true);
            }
        }


    }```Okay game on inning: 1
Thats Miss
Thats Miss
Thats Miss
Three outs game over :( Outs: 3
Thats Miss
Thats Miss
Thats Miss
Thats Miss
Hit Perc: 0.12601973051909088 player: 0.323
Another home run total: 8
Thats Miss
All done
Would you like to start the game (Y/N)---->y
Exception in thread "main" java.util.NoSuchElementException

【问题讨论】:

    标签: java database list linked-list queue


    【解决方案1】:

    首先我注意到代码包含许多冗余和反模式,例如在 while 中定义一个 do/while 是冗余的,然后在 for 循环中交替队列最终将导致 ConcurrentModificationException

    第一个上场的玩家会从队列中移除所有其他玩家,这个程序的目的是什么?

    【讨论】:

    • 我正在尝试制作的小型棒球游戏
    • 你的算法试图做什么,你能解释一下它的目的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多