Question

After both users have guessed the correct answers I have to print out the incorrect guesses for each user at the end. I keep trying to do that but I only get it to print out the incorrect guesses combined for both players on one line instead having them under player 1 and then player 2 incorrect guesses.

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class arrays {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner keyboard = new Scanner(System.in);
        Scanner console = new Scanner(System.in);
        Random rand = new Random();
        System.out.print("Please enter the number of players: ");  //how many people are playing
        int x = console.nextInt();
        System.out.println("Number of players: "+ x);

        ArrayList<Integer> guesses = new ArrayList<Integer>();
        ArrayList<Integer> wrongguesses = new ArrayList<Integer>();
        int userInt = 0;
        int counter = 0;
        int[] players = new int[x];                      //set array to amount of players
        int [] playerAttempts = new int[x];
        boolean playersStillPlaying = true;              //set it so everyone is playing at first
        boolean[] stillPlaying = new boolean[x];         //gives each player a t/f value

        for (int i=0;i<players.length;i++) {            
            int randomNum = rand.nextInt(100) + 1; //generate random numbers depending on the players
            players[i] = randomNum;
            stillPlaying[i] = true;
        }

        while (playersStillPlaying) {                              //sets it so it ends if each person is done playing
            playersStillPlaying = false;
            for (int i = 0 ; i < stillPlaying.length; i++) {
                if (stillPlaying[i]) {
                    System.out.println("Player " + i + ", what's your guess? ");       //ask them for input
                    userInt = keyboard.nextInt();
                    if (userInt == players[i]) {
                        stillPlaying[i] = false;
                        System.out.println(userInt + " You got it!");    //correct
                        guesses.add(userInt + i);
                    }

                    else if (userInt < players[i]) {
                        playersStillPlaying = true;
                        System.out.println(userInt + " is too small");
                        wrongguesses.add(userInt + i);
                    }

                    else if (userInt > players[i]) {
                        playersStillPlaying = true;
                        System.out.println(userInt + " is too big");
                        wrongguesses.add(userInt + i);
                    }
                }
            }  {                
            }

        }
        for (int i = 0; i < playerAttempts.length; i++){
            {
        System.out.println("Player "  + i + " Correct guesses were" + guesses);
        System.out.println("Player "  + i + " Incorrect guesses were" + wrongguesses);
            }
        }
    }
}

No correct solution

OTHER TIPS

You need to have two different arrays to be able to differentiate and add elements withinthem a each user guesses wrong!

ArrayList<Integer> wrongGuesses1= new ArrayList<Integer>();
ArrayList<Integer> wrongGuesses2= new ArrayList<Integer>();

Then figure out in your code who is guessing at that particular instant and if it is wrong then you should add an element to their array respectively!

Although I dont think a user will want to know what their incorrect guesses were, it might just be more efficient to make a counter for the number of wrong guesses there were for each user! :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top