您好,我必须创建一个玩消除游戏的程序。用户必须输入玩家的数量和“循环”的数量或程序计数的数量。因此,例如,如果我输入 8 个玩家和 4 个循环,那么第一个出局的玩家将是玩家 4,然后下一个玩家将是玩家 8,然后它一直持续到有一个赢家。我正在尝试制作一个从 0 开始到用户输入的玩家数量的数字列表。我能够得到一个包含玩家数量的数组,但所有数字都是 0。因此,例如,如果用户输入 4 个玩家,程序应该是“0,1,2,3”,但我的是“0,0,0,0”,我该如何解决这个问题?我创建数组的 while 循环位于第 15 到 19 行,所以我觉得问题出在这些行中。另外,请暂时忽略其余代码,因为我正在尝试在继续之前纠正它。好吧,先谢谢你,这是我的代码
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 count = 0;
int[] array = new int [playersUse];
while(count < playersUse)
{
count++;
arrayUse = Arrays.toString(array);
}
int countUp = 0;
while(countUp < playersUse)
{
countUp++;
}
int addition = 0;
int counter = 0;
while(counter < 999)
{
counter++;
addition++;
if(addition == cyclesUse)
{
}
if (addition > cyclesUse)
{
addition = 0;
}
}
System.out.println(arrayUse);
}
}
最佳答案
据我了解,您正在处理一个空数组。您需要在转换为字符串之前填充它。例如,像这样:
while(count < playersUse)
{
array[count] = count;
count++;
}
arrayUse = Arrays.toString(array);
关于java - 使数组的值成为数字列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299359/