【问题标题】:Adding the sum of array elements and storing them into another array? (Java)添加数组元素的总和并将它们存储到另一个数组中? (爪哇)
【发布时间】: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,因为我将有多个玩家玩这个。我只是给它一个足够的值。

标签: java arrays add store


【解决方案1】:

好的,看来您需要在您的方法中传递当前用户的序号,以便它可以计算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
}

对不起,我还是不明白为什么你需要score 数组,因为你的初始代码与total[i]++ 相同,而你从不读取score 的内容,只写它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2016-08-18
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多