2017-02-02 120 views
1

该程序将输入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; 
} 

}

+2

[输出表的格式在Java的System.out的]的可能的复制(HTTP:/ /stackoverflow.com/questions/2745206/output-in-a-table-format-in-javas-system-out) –

+0

不,不是。 –

+0

你能告诉我们你的问题有什么不同吗? –

回答

1

我看到问题的所在。里面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; 
}