Java 添加数组元素的总和并将其存储到另一个数组中?(爪哇)

Java 添加数组元素的总和并将其存储到另一个数组中?(爪哇),java,arrays,add,store,Java,Arrays,Add,Store,我将要发布的代码片段来自一个程序,该程序应该向用户询问一系列选择题。然后,该程序将保留每个问题的分数,并将总分数存储在另一个数组中。我将有多个玩家玩这个,所以这就是为什么我需要2个阵列 以下是我所拥有的: //Asks questions and stores score in array public static int [] questions () { userinput=""; //input will be stored in here int total[]= ne

我将要发布的代码片段来自一个程序,该程序应该向用户询问一系列选择题。然后,该程序将保留每个问题的分数,并将总分数存储在另一个数组中。我将有多个玩家玩这个,所以这就是为什么我需要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
}

如果答案正确,我想给分数[]中的每个元素加1。然后我想累积分数[]的总数,并将其存储在总数[]的每个元素中。我将total返回到另一个方法,该方法将total存储在一个数组中。

好的,所以您似乎需要在方法中传递当前用户的序号,以便它可以计算出total数组中的正确位置。由于您似乎希望在多个问答环节中汇总总分,因此您需要从外部通过
total

public static void questions(int userOrdinal, int[] total) {
    final int questionsPerUser = 5;

    userinput = ""; //input will be stored in here
    for (int i = 0; i < questionsPerUser; 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!");
            total[userOrdinal * questionsPerUser + i] = 1;
        } else if (!response.equals(ans[i])) // If the answer isn't correct
        {
            total[userOrdinal * questionsPerUser + i] = 0;
            JOptionPane.showMessageDialog(null, "You're wrong!, The correct answer was " + ans[i]);
        }
    } // close loop
}
公共静态无效问题(int userOrdinal,int[]总计){
最后一个智力测验的题目speruser=5;
userinput=”“;//输入将存储在此处
for(int i=0;i

很抱歉,我仍然无法理解您为什么需要
score
array,因为您的初始代码与
total[I]+
相同,并且您从未阅读
score
的内容,只对其进行写入。

您的可疑代码与
totalscore[I]+
相同。此外,
totalscore
有100个元素,而
score
有15个元素。为什么?很抱歉,这是一个复制粘贴错误。我使用的仅有两个数组是score[]和total[]。如果您看到除“total[]或“score[]以外的任何数组,请忽略它们!这仍然让我的问题没有答案,我猜…Total[]的值为100,因为我将有多个玩家,玩这个游戏。我只是给了它一个足够的值。
public static void questions(int userOrdinal, int[] total) {
    final int questionsPerUser = 5;

    userinput = ""; //input will be stored in here
    for (int i = 0; i < questionsPerUser; 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!");
            total[userOrdinal * questionsPerUser + i] = 1;
        } else if (!response.equals(ans[i])) // If the answer isn't correct
        {
            total[userOrdinal * questionsPerUser + i] = 0;
            JOptionPane.showMessageDialog(null, "You're wrong!, The correct answer was " + ans[i]);
        }
    } // close loop
}