如何在没有循环的情况下显示所有分数?

问题描述:

我试图在调查问卷结束时显示我的分数,但是我的做法只显示最后一个分数。如何在没有循环的情况下显示所有分数?

它会工作,如果我把它放在一个循环中,JOptionPane.showMessageDialog,但我只希望它在最后显示一次。

这是我的代码。

//Main method 

JOptionPane.showMessageDialog(null, printPlayerScore(playerName, playerAge, playerScore, playerCount)); 

//printPlayerScore method 

public static String printPlayerScore(String playerName[], int playerAge[], int playerScore[], int playerCount) { 
    String displayResult = ""; 

    for(int i = 0; i < playerCount; i++) { 
     displayResult += "\nName: " + playerName[i] + "\nAge: " + playerAge[i] + "\nScore: " + playerScore[i] + "\n"; 
    } 

    return displayResult; 
} 

实施例运行:

Player1: 12

Player2: 12

当它应该是

Player1: 10

Player2: 12

我知道我需要将方法更改为其他方法,但我该怎么办?

全码:http://pastebin.com/NME8Dh7N

+4

为什么?它看起来像它应该工作。它出什么问题了? – 2014-12-04 15:16:54

+0

它只显示所有玩家的最后一个分数。如果Player1得到5,而Player2得到10,那么它将显示10两个 – Xylus 2014-12-04 15:20:10

+0

你确定Player1的得分是10吗?你有没有试过在调试器中查看它或者做System.out.println(playerScore [0])?你有可能没有正确地将球员比分放入阵列吗? – Jias 2014-12-04 15:20:13

这是的Object-Oriented Programming好处尖叫的例子。 OOP将使这部分代码更易于调试,读取和写入。我会写一些快速代码来解释这个更好(绝不是完美的)。注意我们可以如何使用Player的属性轻松创建一个漂亮的输出字符串。创建一个Player对象的数组,并将其传递给您的打印方法。

public class Player 
{ 
    public String name; 
    public int age; 
    public int score; 

    public String toString() 
    { 
     return String.format("\nName : %s\nAge: %d\nScore: %s\n", name, age, score); 
    } 
} 

public static String printPlayerScore(Player[] players) 
{ 
    String displayResult = ""; 

    for(Player player : players) 
    { 
     displayResult += player.toString(); 
    } 

    return displayResult; 
} 
+1

因为你重写toString,所以'返回Arrays.toString(播放器);' – 2014-12-04 15:50:09

根据你的pastebin,你只有1个数组存储问题的答案。意思是n + 1玩家覆盖n个玩家的答案。

你可以做一个矩阵(阵列数组),或更容易阅读的替代方案,为玩家制作一个班级,并让该班级管理玩家数据,程序只有一个玩家列表。它将减少函数调用中的参数数量,并可以轻松实现答案的个性化。

public class Player { 
    private String name; 
    private List<boolean> answers; 
    private int playerId; 
} 

矩阵会的工作是这样的:

boolean answers[][] = new boolean[playerCount][questionCount]; 

这样一来,每个玩家都有相互independ答案的单独列表。

然后,您只需将每位玩家作为参数发送给功能并根据需要阅读这些参数即可。

我个人不会像你所做的那样对这个问题进行编码,因为它不是面向对象,而且非常混乱。然而,这里有一些你可以使用的东西,以便不会完全破坏你的代码,但可以解决你的问题。

您试图回答的主要问题是为什么最后一个分数出现。正如其他人所说的,你正在使用一个阵列来分析玩家。下面是黑客更改代码得到它的工作:

 public static int[] calculatePlayerScore(boolean userAnswer[], int playerScore[], int playerCount) { 

     for (int i = 0; i < 1; i++) { 
      playerScore[i] = 0; 
      for (int ii = 0; ii < userAnswer.length; ii++) { 
       if (userAnswer[ii]) { 
        playerScore[i] += 1; 
       } 
      } 
     } 

     return playerScore; 

    } 

而且:需要改变

 List<int[]> playerScoresList = new ArrayList<int[]>(); 
     for (int i = 0; i < playerCount; i++) 
     { 
      int playerScore[] = new int[1]; 
      JOptionPane.showMessageDialog(null, "It is " + playerName[i] + "'s turn now!"); 

      checkQuestion(question, questionAnswer, userAnswer); 

      System.out.println("Name: " + playerName[i] + " || Age: " + playerAge[i] + "\n\n ~~~~ Results ~~~~"); 
      System.out.println(printQuestionnaireResults(question, userAnswer) + " ~~~~ End of Results ~~~~\n"); 

      playerScore = calculatePlayerScore(userAnswer, playerScore, playerCount); 
      // double playerScorePercentage = ((double)playerScore[i]/(double)question.length) * 100; 
      double playerScorePercentage = ((double)playerScore[0]/(double)question.length) * 100; 

      System.out.println(playerName[i] + " got " + playerScore[0] + " questions correct out of " + question.length + "! (" + 
        playerScorePercentage + "%)\n"); 

      playerScoresList.add(playerScore); 
     } 

     JOptionPane.showMessageDialog(null, printPlayerScore(playerName, playerAge, playerScoresList, playerCount)); 

其他方法

 public static String printPlayerScore(String playerName[], int playerAge[], List<int[]> playerScore, int playerCount) { 

     String displayResult = ""; // Maybe use StringBuilder 

     for(int i = 0; i < playerCount; i++) 
     { 
      int[] score = playerScore.get(i); 
      displayResult += "\nName: " + playerName[i] + "\nAge: " + playerAge[i] + "\nScore: " + score[0] + "\n"; 
     } 

     return displayResult; 
    } 

现在,您将创建一个新的每个用户每次都有一个元素的数组。

请记住,这个黑客程序提供的代码应该在你的目前的代码中有最小的变化。你应该认真考虑重写IMO的整个程序。我也注释了一些其他代码以及它的工作。