java - 队列不循环,提前终止?

标签 java arrays queue

我正在尝试创建一个增强的 pig 游戏,用户可以在其中掷用户输入的骰子数。如果掷出的所有骰子都是一个,那么您将失去您的银行积分,如果其中一个是一个,您将没有积分,您的银行是安全的。否则,您可以将滚动的点数存入银行。我试图循环队列以便每个玩家轮到他们,但它只是循环队列中的同一个人询问玩家想要掷多少骰子,然后得到结果并终止。我在这里做错了什么?

另外还有当前的控制台输出:

enter image description here

import java.util.Scanner;
import java.util.stream.IntStream;   
import javax.lang.model.element.Element;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

public class EnhancedGameOfPig {
    static int count;

    static int roll() {
        return (int) (6.0 * Math.random()) + 1;
    }

    public static void play() {

        Scanner sc = new Scanner(System.in);
        Queue<Player> myQueue = new LinkedList<Player>();
        System.out.println("How many players are there? (2-10 Max)");
        int numOfPlayers = sc.nextInt();
        sc.nextLine();
        for (int i =0; i < numOfPlayers; i++) { // creates specified number of
                                                    // players and adds
                                                    // them to a queue
            System.out.println("What is your name player " + (i+1) + "?");
            String playerName = sc.nextLine();
            Player player = new Player(playerName, 0);
            myQueue.add(player);
        }

        System.out.println("How many points would you like to play to, to win?"); // sets
                                                                                    // the
                                                                                    // number
                                                                                    // of
                                                                                    // points
                                                                                    // required
                                                                                    // to
                                                                                    // win
                                                                                    // the
                                                                                    // game
        int maxPoints = sc.nextInt();
        sc.nextLine();

        for (Player e : myQueue) {
            System.out.println("How many dice would you like to roll " + myQueue.peek().getPlayerName() + " ?");
            int numofDice = sc.nextInt();
            sc.nextLine();
            int[] diceArray = new int[numofDice]; // creates an array to hold
                                                    // values of each dice roll
            for (int i = 0; i <= numofDice-1; i++) {
                diceArray[i] = roll(); // rolls dice and adds dice roll values
                                        // to array
                if (diceArray[i] == 1) {
                    count++;
                }
            }
            int first = diceArray[0]; // looks at first value of array
            for (int element : diceArray) {
                if (element == first && first == 1) { // checks to see if all
                                                        // rolls were 1's
                    System.out.println("All of the dice you rolled were ones! You loose all your banked points!");
                    myQueue.peek().setBankedPoints(0);
                    break;
                }

            }
            if (count == 1) {
                System.out.println("You rolled a one, so you don't get any points. Sorry!");
            } else {
                int sum = IntStream.of(diceArray).sum();
                System.out.println(sum + " points have been added to your bank!");
                myQueue.peek().bankedPoints += sum;
            }
        }
    }
}

最佳答案

您的循环控件遍历队列中的每个玩家。

for (Player e : myQueue) {

但是在整个循环体中,您只引用队列中的第一个玩家,使用 myQueue.peek()。例如:

System.out.println("How many dice would you like to roll " 
   + myQueue.peek().getPlayerName() + " ?");

peek() 方法返回队列中的第一个玩家,但您正试图影响玩家 e。您可以通过在整个循环体中使用 e 而不是 myQueue.peek() 来解决这个问题。

关于java - 队列不循环,提前终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33530263/

相关文章:

python - python中arange的奇怪行为

php - 如何将多个可重复字段保存为数据库中的数组?

java - 寻址未知服务器 ActiveMQ 时连接停止

java - 如何调用 ArrayList 中数组的方法?

c++ - 使用数组对带有文本图像输出的物理线进行建模

java - 不满意的链接器错误 : library file not found

添加到队列时在 C 和 Malloc 中创建队列

php - Laravel 5.3 电子邮件队列中不允许序列化 'Closure'

java - 使用 jaybird 2.1.6 在 blob 字段中插入文件

java - 如何在 Java 中获取属于另一个具有特定属性的标签的 XML 标签?