1

I would like to know how I can do a for loop to do a for loop in order to display the players names names alongside the rolls of their dice depending on the number of players entered by the user.

if (num_of_players == 1) {
    players[0] = player1;
}
if (num_of_players == 2) {
    players[0] = player1;
    players[1] = player2;
}
if (num_of_players == 3) {
    players[0] = player1;
    players[1] = player2;
    players[2] = player3;
}
if (num_of_players == 4) {
    players[0] = player1;
    players[1] = player2;
    players[2] = player3;
    players[3] = player4;
}
if (num_of_players == 5) {
    players[0] = player1;
    players[1] = player2;
    players[2] = player3;
    players[3] = player4;
    players[4] = player5;
}
if (num_of_players == 6) {
    players[0] = player1;
    players[1] = player2;
    players[2] = player3;
    players[3] = player4;
    players[4] = player5;
    players[5] = player6;
}

for (String y : players) {
    JOptionPane.showMessageDialog(null, "The first dice rolls " + y);
12
  • 1
    You can't do this in a for loop, because of your variables player1, player2 etc. But you can write if (num_of_players >= 1) players[0] = player1; if (num_of_players >= 2) players[1] = player2; .... Commented Feb 18, 2017 at 12:40
  • Seems like there might be something flawed with your design if you have a seperate variable for each player named playerN. Show a bit more of the code to provide some context, and you'll get more helpful answers. Commented Feb 18, 2017 at 12:43
  • What do you mean rolls of their dice? Please elaborate exactly what you need help with. Commented Feb 18, 2017 at 12:45
  • Why aren't you using a loop to read the names directly into the array? Commented Feb 18, 2017 at 12:45
  • @HerbWolfe A for loop can't be used for input here because the indices are being fed by different variables named player1, player2, etc. Commented Feb 18, 2017 at 12:47

2 Answers 2

3

Since you've already got 6 player variables, you can just put these into an array, and then take a copy of part of the array. There's no explicit loop required:

String[] aPlayers = {player1, player2, player3, player4, player5, player6};
System.arraycopy(aPlayers, 0, players, 0, num_of_players);

Note that you're only setting the first num_of_players elements of players, you will have null elements at the end (or whatever happens to be in those elements already); and the for loop will still iterate over those elements.

If this is not what you want, you might want to consider assigning a smaller list to players:

players = Arrays.copyOfRange(aPlayers, 0, num_of_players);

Or you can wrap the array in a list and take a sublist:

for (String y : Arrays.asList(players).subList(0, num_of_players)) {
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure loops are applicable here, but this simplification is possible, given each block repeats the work of the previous ones...

if (num_of_players == 1) {
    players[0] = player1;
}
if (num_of_players >= 2) {
    players[1] = player2;
}
if (num_of_players >= 3) {
    players[2] = player3;
}
if (num_of_players >= 4) {
    players[3] = player4;
}
if (num_of_players >= 5) {
    players[4] = player5;
}
if (num_of_players >= 6) {
    players[5] = player6;
}

Another way to tackle this would to be create an array and use that with your loop. Better still player1, player2 variables would be an array to start with.

String [] playersArray = {player1, player2, player3, player4, player5, player6};

for (int i = 0; i< num_of_players; i++) {
    players[i] = playersArray[i];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.