队列没有循环,并提前终止?

时间:2015-11-04 19:19:39

标签: java arrays queue

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

此处还有当前的控制台输出:

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

1 个答案:

答案 0 :(得分:3)

你的循环控制遍历队列中的每个玩家。

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()来解决此问题。