java - 评分系统和数组

标签 java arrays procedural

我的程序不断将每个玩家的分数相加,而不是将其分开,例如,如果第一个玩家获得 3/5,第二个玩家获得 2/5,则第二个玩家的分数显示将为 5。我知道答案可能非常简单,但我无法在代码中找到它。

public static void questions(String[] question, String[] answer, int n) {

    String[] name = new String[n];  // Player Names
    int[] playerscore = new int[n]; // Argument for Score
    String[] que = new String[question.length]; //Questions for Loops
    int score = 0; // Declare the score

    /* --------------------------- For loop for number of players --------------------------- */
    for (int i = 0; i < n; i++) {
        name[i] = JOptionPane.showInputDialog("What is your name player" + (i + 1) + "?");
        JOptionPane.showMessageDialog(null, "Hello :" + name[i] + " Player number " + (i + 1) + ". I hope your ready to start!");

        /* --------------------------- Loop in Loop for questions --------------------------- */
        for (int x = 0; x < question.length; x++) {
            que[x] = JOptionPane.showInputDialog(question[x]);

            if (que[x].equals(answer[x])) {
                score = score + 1;
            } else {
                JOptionPane.showMessageDialog(null, "Wrong!");
            }

        } // End for loop for Question
        playerscore[i] = score;
        System.out.println("\nPlayer" + (i) + "Name:" + name[i] + "\tScore" + score);
    }
}

最佳答案

在每个玩家开始之前,您需要将分数重置为 0。

在每个玩家的循环之后添加以下内容:

score = 0;

或者,您可以直接在数组中增加分数。只需更改:

score = score + 1;

至:

playerscore[i] = playerscore[i] + 1;

或者简单地说:

playerscore[i]++;

关于java - 评分系统和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27302416/

相关文章:

java - 通过java中的socket编程检查远程服务器是否处于监听模式

javascript - 如何将普通数组转换为 Float32Array?

arrays - 通过唯一键合并 Ruby 中的两个数组

c - c中的json数组解析

oop - 设计WCF服务接口(interface)。过程式设计与面向对象设计

java - 如果有人可以处理这个错误代码,我会很高兴,找不到问题所在

java - 写一个从 servlet 到 JavaScript 的数组响应?

java - 如何最有效地共享 log4j 库

php - 我是用对象还是 OOP 编写过程代码?

php - PHP中的声音生成或声音文件连接