简体   繁体   中英

How can I get my array to replace a value more than once?

I have to create a program that plays an elimination game.

The user has to input the number of players and the number of "cycles" or how much the program counts by.

So for example if I input 8 players and 4 cycles then the first player out will be player 4 and then the next one would be player 8, and then it goes on and on until there is one winner.

As of now my current plan to change any variables that the cycle lands on to 999 and keep going until there is one winner. But right now my code only changes one value to 999 and then stops.

How can I get my code to change any value it lands on to 999 and keep going until a winner has been picked? I know it's because of the code on line 33 that tells it to change the same variable over and over. But how can I change it so it does it more then once? Here is my code

public class numberPicker 
 {
     public static void main(String[] args) 
     {
         String cycles = JOptionPane.showInputDialog("Please input the number of cycles");
         int cyclesUse = Integer.parseInt(cycles);
         String players = JOptionPane.showInputDialog("Please input the number of players");
         int playersUse = Integer.parseInt(players);
         String arrayUse = "";
         int count = 0;

         int[] array = new int [playersUse];


         while(count < playersUse)
         {
             array[count] = count;
             count++;
         }


         int addition = 0;
         int counter = 0;

         while(counter < 999)
         {
             addition++;

             int stuff = array[cyclesUse]; 
             array[stuff-1] = 999;

             arrayUse = Arrays.toString(array);
             System.out.println(arrayUse);


             if (addition > cyclesUse)
             {
                 addition = 0;
             }
          }
          arrayUse = Arrays.toString(array);
          System.out.println(arrayUse);
     }
 }

问题是您要更新相同的数组位置,需要更改循环使用的值,或者您可以使用其他变量

A few things are causing problems here. First, the condition in your second while loop, counter < 999 has no way of returning false. Therefore, the loop will keep running without an end so you should update counter in a way that will eventually terminate the loop. Second, cyclesUse is never being updated in the loop, so the loop is just reupdating the same variable in the array to 999. To implement your algorithm, however, I recommend choosing one of the players at random in each iteration of the while loop and setting its value to 999. Within this while loop should have another while loop that finds a valid random player that does not have a value of 999. Here is what it should look like,

int counter = 0;

 while(counter < count-1)
 {

     int randomIndex = (int) Math.random()*count; //picks a random number between 0 and 8
     while(array[randomIndex] == 999){
          randomIndex = (int) Math.random()*count; //keep finding a random index until one is found whose value is not 999
     }
     array[randomIndex] = 999;
     arrayUse = Arrays.toString(array);
     System.out.println(arrayUse);

     counter++; //update the counter so that the loop can eventually end
  }

Assuming I understand the rules of the game correctly, the issue is that your array index isn't being incremented properly. (I take it the player set to 999 is not to be random, but rather in increments of the cycle variable.) See if this makes sense:

import java.util.*;
import javax.swing.JOptionPane;
public class numberPicker 
 {
     public static void main(String[] args) 
     {
         String cycles = JOptionPane.showInputDialog("Please input the number of cycles");
         int cyclesUse = Integer.parseInt(cycles);
         String players = JOptionPane.showInputDialog("Please input the number of players");
         int playersUse = Integer.parseInt(players);
         String arrayUse = "";

         int[] array = new int [playersUse];
         for(int i = 0; i < playersUse; i++){
             array[i] = i+1;
         }

         int counter = 0;
         int shift = -1;
         int playersRemoved = 0;

         while(true)
         {

             counter++;

             if((cyclesUse*counter)+shift >= array.length){
                 counter = 0;
                 shift++;
             }

             array[(cyclesUse*counter)+shift] = 999;
             System.out.println(Arrays.toString(array));

             playersRemoved++;

             if(playersRemoved >= playersUse){
                 break;
             }

          }

         System.out.println("Congratulations, player " +     ((cyclesUse*counter)+shift+1) + " has won!");

     }

 }

(Briefly tested- may not work exactly how you want, but it should demonstrate the point)

So first off, in the main loop, counter is incremented. This is to keep track of how many jumps have been made. In this case, when it hits the end of the list, it will return to the beginning, but increment shift by 1. (This is to prevent it from getting stuck assigning the same players to 999 over and over again.) playersRemoved is just to keep track of when the loop should end, and display the last player that was assigned to.

Play with it a little, and hopefully that will give you a feel for the concept. (To be honest this was more complex than I originally thought)

I rewrite the loop part and this is the output:

Out 4th player(array[3])    [0, 1, 2, 999, 4, 5, 6, 7]
Out 8th player(array[7])    [0, 1, 2, 999, 4, 5, 6, 999]
Out 5th player(array[4])    [0, 1, 2, 999, 999, 5, 6, 999]
Out 2th player(array[1])    [0, 999, 2, 999, 999, 5, 6, 999]
Out 1th player(array[0])    [999, 999, 2, 999, 999, 5, 6, 999]
Out 3th player(array[2])    [999, 999, 999, 999, 999, 5, 6, 999]
Out 7th player(array[6])    [999, 999, 999, 999, 999, 5, 999, 999]
The winer is 6th player(array[5])

My code is:

    int remainNum = playersUse;
    int loopCount = 0;
    int playerCount = 0;

    while (remainNum > 1) {
        int pos = ++loopCount % playersUse;
        int playerPos = pos == 0 ? playersUse - 1 : pos - 1;
        playerCount++;
        if (array[playerPos] == 999) {
            playerCount--;
            continue;
        }
        if (playerCount % cyclesUse == 0) {
            array[playerPos] = 999;
            remainNum--;
            System.out.println(String.format("Out %dth player(array[%d])\t%s", playerPos + 1, playerPos, Arrays.toString(array)));
        }
    }
    for (int i = 0; i < array.length; i++) {
        if (array[i] != 999) {
            System.out.println(String.format("The winer is %dth player(array[%d])", i + 1, i));
            break;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM