首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何让我的数组多次替换一个值?

如何让我的数组多次替换一个值?
EN

Stack Overflow用户
提问于 2016-03-31 12:53:49
回答 4查看 89关注 0票数 0

我必须创建一个可以玩淘汰赛的程序。

用户必须输入播放器的数量和“循环”的数量,或者程序的计数。

例如,如果我输入8个玩家和4个循环,那么第一个玩家将是玩家4,然后下一个玩家将是玩家8,然后它继续下去,直到有一个赢家。

到目前为止,我目前的计划是将周期落在999上的任何变量都更改为999,并继续下去,直到有一个赢家。但是现在,我的代码只将一个值更改为999,然后停止。

我怎样才能让我的代码将它到达的任何值更改为999,并继续执行,直到选出获胜者?我知道这是因为33行的代码告诉它一遍又一遍地改变相同的变量。但是,我如何才能更改它,使其不止一次地执行呢?以下是我的代码

代码语言:javascript
代码运行次数:0
运行
复制
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);
     }
 }
EN

回答 4

Stack Overflow用户

发布于 2016-03-31 13:10:14

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

票数 1
EN

Stack Overflow用户

发布于 2016-03-31 13:29:52

有几件事在这里引起了问题。首先,第二个while循环中的条件,counter < 999没有办法返回false。因此,循环将继续运行而不会结束,因此您应该以一种最终终止循环的方式更新计数器。其次,cyclesUse永远不会在循环中更新,所以循环只是将数组中的相同变量重新更新为999。但是,为了实现您的算法,我建议在while循环的每次迭代中随机选择一个玩家,并将其值设置为999。在这个while循环中,应该有另一个while循环,用于查找不具有值999的有效随机玩家。下面是它应该是什么样子的,

代码语言:javascript
代码运行次数:0
运行
复制
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
  }
票数 0
EN

Stack Overflow用户

发布于 2016-03-31 14:08:34

假设我正确地理解了游戏规则,那么问题就是您的数组索引没有正确地递增。(我认为设置为999的播放器不是随机的,而是以循环变量的增量。)看看这是否有意义:

代码语言:javascript
代码运行次数:0
运行
复制
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!");

     }

 }

(简单测试-可能不是完全按照你想要的方式工作,但它应该证明这一点)

所以首先,在主循环中,计数器递增。这是为了跟踪已经进行了多少次跳跃。在这种情况下,当它到达列表的末尾时,它将返回到开始,但会递增移位1。(这是为了防止它一次又一次地将相同的玩家分配给999。)playersRemoved只是跟踪循环何时结束,并显示分配到的最后一个玩家。

尝试一下它,希望这能让你对这个概念有一个感觉。(老实说,这比我最初想象的要复杂得多)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36324449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档