【发布时间】:2012-01-14 10:48:08
【问题描述】:
我要发布的代码片段来自一个程序,该程序应该询问用户一系列多项选择题。然后程序将记录每个问题的分数,并有一个将存储在另一个数组中的总数。我会有多个玩家玩这个,所以我需要 2 个数组。
这是我所拥有的:
//Asks questions and stores score in array
public static int [] questions ()
{
userinput=""; //input will be stored in here
int total[]= new int[100];
int score[]=new int[5];
for(int i=0; i < ps.length; i++)
{
userinput=JOptionPane.showInputDialog(que[i]); //Outputs a question stored in another array in another method.
if (response.equals(ans[i])) //this compares the user input to the correct answer of the question, which is in another method.
{
JOptionPane.showMessageDialog(null,"You selected " + " " + ans[i] + " You were correct, 1 point!");
score[i]=1;
total[i]=total[i]+score[i];
}
else if(!response.equals(ans[i])) // If the answer isn't correct
{
score[i]=0; // I want to assign 0 for the question
JOptionPane.showMessageDialog(null,"You're wrong!, The correct answer was "+ans[i]);
}
} // close loop
return total; // return's this to another method which will do all of the other work
}
我似乎在这里遇到了问题:
JOptionPane.showMessageDialog(null,"You selected " + " " + ans[i] + " You were correct, 1 point!");
score[i]=1;
total[i]=total[i]+score[i];
如果答案正确,我想给 score[] 中的每个元素加 1。然后我想累积分数 [] 的总和并将其存储在总 [] 的每个元素中。我将总计返回给另一个将其存储在数组中的方法。
【问题讨论】:
-
您的可疑代码与
totalscore[i]++相同。此外,totalscore有 100 个元素,而score有 15 个元素。为什么? -
抱歉,这是一个复制和粘贴错误。我正在使用的仅有 2 个数组是 score[] 和 total[]。
-
如果您看到除“total[]”或“score[]”之外的任何数组,请忽略它们!
-
这仍然没有回答我的问题,我猜...
-
Total[] 的值为 100,因为我将有多个玩家玩这个。我只是给它一个足够的值。