How can I store and add items back to a queue

I don't know how to store the items from the first queue, save them and then add them back on

right now it goes through once and then the queue is empty

I know the concept just don't know the syntax

tried making a second queue and adding the players back when the queue is empty but it didn't work

        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

1 answer

  • answered 2021-10-12 16:08 Yassir Khaldi

    First of all I noticed that the code contains many redundancies and antipatterns, for example defining a do/while inside a while is reduntant, then alternating the queue inside the for loop will eventually result in a ConcurrentModificationException.

    The first player that plays removes all other players from the Queue, what's the purpose of this program?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum