Java编程中的数组
所以我需要制作一个程序,将用户和计算机输入存储到数组中,但当我打印结果时,它只显示最后输入的值。我无法让数组存储用户和计算机输入的所有值。 以下是部分代码:Java编程中的数组,java,arrays,Java,Arrays,所以我需要制作一个程序,将用户和计算机输入存储到数组中,但当我打印结果时,它只显示最后输入的值。我无法让数组存储用户和计算机输入的所有值。 以下是部分代码: do { String[] user = new String[100]; yourChoice = GetValidUserInput(promptString); for(int u = 0; u < user.length; u++) { user[u] = yourChoice; } rand
do
{
String[] user = new String[100];
yourChoice = GetValidUserInput(promptString);
for(int u = 0; u < user.length; u++)
{
user[u] = yourChoice;
}
randomNum = (int) (Math.random() * 3);
String[] computer = new String[100];
computerChoice = GenerateComputerChoice(randomNum);
for(int c = 0; c < computer.length; c++)
{
computer[c] = computerChoice;
}
TheVictor(yourChoice, computerChoice);
decide = JOptionPane.showInputDialog("Would you like to play another game?");
if(decide.equals("no"))
{
Results(user, computer);
}
}while(!decide.equals("no") || decide.equals("yes"));
do
{
字符串[]用户=新字符串[100];
yourChoice=GetValidUserInput(promptString);
对于(int u=0;u
看起来您需要移动循环中获取输入的行
for(int u = 0; u < user.length; u++)
{
yourChoice = GetValidUserInput(promptString);
user[u] = yourChoice;
}
for(int c = 0; c < computer.length; c++)
{
computerChoice = GenerateComputerChoice(randomNum);
computer[c] = computerChoice;
}
for(int u=0;u
请尝试上面的代码。。它会起作用的。如果您有任何疑问,请在评论中询问。作为评论,您的while声明似乎是多余的。您可能想重新编写它(decision.equalsIgnoreCase(“yes”))谢谢这确实有效,我想知道有没有办法将两个一维数组变成一个二维数组?在这段代码中,它们与您的代码相比有很多变化。我理解你的游戏,然后写了代码。请不要忘记将此标记为已接受的答案,以供将来的访问者使用。回答你的问题是的,你可以把两个一维数组变成一个二维数组。只需创建一个字符串[n][2]并在其中循环,字符串[i][0]=用户[i]和字符串[i][1]=计算机[i]
List usersList = new ArrayList();
List compList = new ArrayList();
do
{
yourChoice = GetValidUserInput(promptString);
usersList.add(yourChoice);
randomNum = (int) (Math.random() * 3);
computerChoice = GenerateComputerChoice(randomNum);
compList.add(computerChoice);
TheVictor(yourChoice, computerChoice);
decide = JOptionPane.showInputDialog("Would you like to play another game?");
}while(!decide.equals("no") || decide.equals("yes"));
String[] user = (String[])usersList.toArray(new String[usersList.size()]);
String[] computer = (String[])compList.toArray(new String[compList.size()]);
if(decide.equals("no"))
{
Results(user, computer);
}