java - 二维数组的输出总计

标签 java arrays multidimensional-array

该程序将输入 4 名玩家的分数,当您输入分数时,它应该在右侧显示当前的节目,如下图所示。 我无法使用 System.out.format,因为我没有使用数据库中的数据

它应该是什么样子:

enter image description here

我有我认为应该是它的逻辑,我只是​​不知道如何输出它,所以它看起来和图片一样。 这是到目前为止的代码:

private static final int NUMBER_OF_ENDS = 10;
private static String[] player = new String[4];
private static int[][] scores = new int[4][NUMBER_OF_ENDS];
private static int totalsOne = 0;
private static int totalsTwo = 0;
private static int totalsThree = 0;
private static int totalsFour = 0;


public static void addPlayer() {
    //for loop to add player to player array
    Scanner input = new Scanner(System.in);
    for (int counter = 0; counter < player.length; counter++) {
        System.out.println("Enter player #" + counter + " name");
        player[counter] = input.nextLine();

    }
}

public static void addScores() {
    //for loops to add scores to particular end and show user current score
    String output = "";
    Scanner input = new Scanner(System.in);
    for (int inner = 0; inner < NUMBER_OF_ENDS; inner++) {
        for (int counter = 0; counter < player.length; counter++) {

            System.out.print("Enter score number " + (inner + 1) + " score for " + player[counter] + "---->");
            scores[counter][inner] = input.nextInt();
            System.out.println("Current Scores-\n");
            System.out.println(getResults(inner));
            if (counter ==0){
                totalsOne += scores[counter][inner];

            }else if(counter ==1){
                totalsTwo += scores [counter][inner];
            }else if(counter ==2){
                totalsThree += scores [counter][inner];
            }else if(counter ==3){
                totalsFour += scores [counter][inner];
            }
        }
    }
}

private static String getResults(int currentEnd) {
    //format current results 
    String result = "";
    for (int count = 0; count < player.length; count++) {
        result += player[count];

        for (int i = 0; i <= currentEnd; i++) {
            result += "\t" + scores[count][i];
        }

        result += "\n";

    }
    return result;
}

}

最佳答案

我明白问题出在哪里了。在 addScores() 方法中,您需要创建 4 个新的 int 实例,您可以在其中保存每 4 个玩家的分数,如下所示:

private static String getResults(int currentEnd) {      
    int player1totalscore = 0; //4 instances of int where each player's score will be stored.
    int player2totalscore = 0;
    int player3totalscore = 0;
    int player4totalscore = 0;

    for (int count = 0; count < player.length; count++) {           
        for (int i = 0; i <= currentEnd; i++) {
            if(count == 0) //when first player
                player1totalscore += scores[count][i];  //add score
            if(count == 1) //when 2nd player
                player2totalscore += scores[count][i];  //add score
            if(count == 2) //when 3rd player
                player3totalscore += scores[count][i];  //add score
            if(count == 3) //when 4th player
                player4totalscore += scores[count][i];  //add score            
        }           
    }
    String result = "";
    for (int count = 0; count < player.length; count++) {
        result += player[count];
        for (int i = 0; i <= currentEnd; i++) {             
            if(i==currentEnd){   //when the string is at player's last score entry, add his/her score at the end            
            if(count == 0)
                result += "\t" + scores[count][i] + "\t" + " | " + player1totalscore;
            if(count == 1)
                result += "\t" + scores[count][i] + "\t" + " | " + player2totalscore;
            if(count == 2)
                result += "\t" + scores[count][i] + "\t" + " | " + player3totalscore;   
            if(count == 3)
                result += "\t" + scores[count][i] + "\t" + " | " + player4totalscore;
            } else { //otherwise, keep adding individual scores
                result += "\t" + scores[count][i];
            }               
        }            
        result += "\n";            
    }
    return result;
}

关于java - 二维数组的输出总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42004344/

相关文章:

C# - 带矩阵的文本文件 - 划分所有条目

arrays - 我怎样才能在 Swift 中读取这个多维数组呢?

javascript - 如何对两个多维数组的元素求和?

Java,复制数组的大小

javascript - 从数组生成所有不同的非空子数组,并保留顺序

java - JPA+Hibernate - 实体关系中的循环 - 级联策略

java - 在 Linux 上开始使用 JMX 身份验证时无法连接到 JMX

java - java中如何实现正则表达式替换功能?

java - PaintComponent 不绘制图 block 或绘制字符串

javascript - 从 JavaScript 中的数组中删除特定字符串