2015-11-30 2 views
-2

Это моя задачаКак я могу добавить значения в каждой строке массива?

enter image description here

У меня есть программа, распечатывания значения в массивах, этот шаг не должен быть там, то есть последний цикл. Вместо этого мне нужно добавить строки вверх, значение по значению, используя класс die, который у меня есть, код - something.getFaceValue() ;.

Кодекс Ниже

import java.util.Scanner; 

class ASgn8 
{ 

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 

    System.out.print("How many players? "); 
    int playerCount = scan.nextInt(); // get number of participant player... 
    scan.nextLine(); 

    Die[] tempDie = new Die[5]; // temporary purpose 
    Die[][] finalDie = new Die[5][]; // final array in which all rolled dies stores... 
    int tempVar = 0; 

    String [] playerName = new String[playerCount]; // stores player name 

    int totalRollDie = 0; // keep track number of user hash rolled dies... 

    for(int i = 0; i < playerCount; i++) // get all player name from command prompt... 
    { 
     System.out.print("What is your name: "); 
     String plyrName = scan.nextLine(); 
     playerName[i] = plyrName; 
    } 

    for(int i = 0; i < playerCount; i++) 
    { 

     System.out.println(playerName[i] + "'s turn...."); 
      totalRollDie = 0; 
      Die d = new Die(); 
      System.out.println("Rolled : " + d.roll()) ; 
      tempDie[totalRollDie] = d; 
      totalRollDie++; 
      System.out.println("Want More (Yes/No) ???"); 
      int choice = scan.nextInt(); 

       while(choice == 1) 
       { 
        if(totalRollDie < 5) 
        { 
        Die dd = new Die(); 
        System.out.println("Rolled : " + dd.roll()) ; 
        tempDie[totalRollDie] = dd; 
        totalRollDie++; 
        System.out.println("Want More (Yes/No) ???"); 
        choice = scan.nextInt(); 
       } 
      } 

      finalDie[i] = new Die[totalRollDie]; 
      for(int var = 0 ; var < totalRollDie ; var++) 
      { 
       finalDie[i][var] = tempDie[var]; 
      } 
    } 

    for(int i = 0 ;i < playerCount ; i++) //prints out the values stored in the array. need to sum them instead. 
    { 
     System.out.println(" --------- " + playerName[i] + " ------------ "); 

     for(Die de : finalDie[i]) 
     { 
      System.out.println(de); 
     } 
    } 


    tempDie = null; 

} 
} 

Кто-нибудь есть какие-либо советы?

EDIT: Победить класс

public class Die 
{ 
    private final int MAX = 6; 

    private int faceValue; 


    public Die() 
    { 
     faceValue = 1; 
    } 


    public int roll() 
    { 
     faceValue = (int)(Math.random() * MAX) + 1; 

     return faceValue; 
    } 

    public void setFaceValue(int value) 
    { 
     faceValue = value; 
    } 


    public int getFaceValue() 
    { 
     return faceValue; 
    } 


    public String toString() 
    { 
     String result = Integer.toString(faceValue); 

    return result; 
    } 
} 
+0

Где твой Die класс? – Perdomoff

+0

Отредактировано в OP –

ответ

1

Append следующий код в код существования,

public static void main(String... s) 
{ 
...... 

int score[][] = new int[playerCount][2]; 

     for(int i = 0 ;i < playerCount ; i++){ // finally print whatever user's roll value with all try... 
     // System.out.println(" --------- " + playerName[i] + " ------------ "); 
      int playerTotalScore = 0; 
      for(Die de : finalDie[i]){ 
      // System.out.println(de); 
       playerTotalScore += de.getFaceValue(); 
      } 
      score[i][0] = i; 
      score[i][1]=playerTotalScore; 
     } 

     tempDie = null; 

     System.out.println("------------- Participant Score Card -------------"); 
     System.out.println("Index PlayerName Score"); 
     for(int i = 0 ; i < score.length ; i++){ 
      System.out.println(score[i][0] + "  -  " + playerName[score[i][0]] + "  -  " + score[i][1]); 
     } 

     int temp[][] = new int[1][2]; 
     for(int i = 0 ; i< score.length; i++){ 
      for(int j = i+1 ; j<score.length;j++){ 
       if(score[i][1] < score[j][1]){ 
        temp[0][0] = score[i][0]; 
        temp[0][1] = score[i][1]; 
        score[i][0] = score[j][0]; 
        score[i][1] = score[j][1]; 
        score[j][0] = temp[0][0]; 
        score[j][1] = temp[0][1]; 
        } 
      } 
     } 


     System.out.println("--------------------------------------------------------"); 
     System.out.println("-----------------WINNER---------------------"); 
      System.out.println(score[0][0] + " - " + playerName[score[0][0]] + " - " + score[0][1]); 
     System.out.println("--------------------------------------------------------"); 

} // end of main method... 
0

Если я правильно понял ваш вопрос правильно, вы можете сохранить значение в переменной;

int[] mPlayerScores = new int[playerCount]; 
String name = ""; 

for(int i = 0 ;i < playerCount ; i++) //prints out the values stored in the array. need to sum them instead. 
{ 

    int playerScoreSum = 0; 
    name = playerName[i]; 
    System.out.println(" --------- " + name + " ------------ "); 

    for(Die de : finalDie[i]) 
    { 
     playerScoreSum += de; 

    } 

// this should store the sums in an array 
mPlayerScores[i] = playerScoreSum; 

//display the result for each player outside the for-loop to avoid continous printing 
System.out.println(playerScoreSum); 

} 

    //finds the highest score 
    double max = mPlayerScores[0]; 

    for (int j = 1; j< mPlayerScores.length; j++) 
    { 
    if (mPlayerScores[j] > max) 
    { 
     max = mPlayerScores[j]; 

    } 
    } 
System.out.println(name+" is the winner with " + max+ " score"); 
+0

Это работает для одного игрока, однако мне нужно, чтобы он хранился для двух или более игроков, поэтому я могу их сравнить. –

+0

Хорошо. Я только что отредактировал свой ответ –

Смежные вопросы