1

I have an array with 3 players and an other array with several powers.

String[] array_Player = {"Celine", "Amelia", "Olivia"};
int[] array_power_1 = {4,2,10};

The player who has the smallest "power" will have 2 points, the best will have 6 pts.

I would like to get this result:

Celine | power 4 => points 4 
Amelia | power 2 => points 2 
Olivia | power 10 => points 6 

Here is my resultat for now:

Celine | power 4 => points 2
Amelia | power 2 => points 4 
Olivia | power 10 => points 6 

My points are not correctly attributed.

I think my array_point_power() method is not correct?

public static void array_point_power(String[] array_Player, int[] array_point){

      int points = 2;

      for(int i=0; i<array_Player.length; i++){
          array_point[i] = points;
          points = points + 2;
      }
  }

Here is my code:

import java.util.*;

class Main {
  public static void main(String[] args) {
    
    String[] array_Player = {"Celine", "Amelia", "Olivia"};
    int[] array_power_1 = {4,2,10};
    int[] array_point_1 = new int[3];
    
    System.out.println("Round 1 : ");
    array_point_power(array_Player, array_point_1);
    affichage_round(array_Player, array_power_1, array_point_1);

  }

   public static void array_point_power(String[] array_Player, int[] array_point){

      int points = 2;

      for(int i=0; i<array_Player.length; i++){
          array_point[i] = points;
          points = points + 2;
      }
  }


  public static void affichage_round(String[] array_Player, int[] array_power, int[] array_point) {

    for(int i=0; i<array_Player.length; i++){
            System.out.println("Joueur " + array_Player[i] + " | Puissances " +  array_power[i] + " | Points " + array_point[i] );
        }

  }

}

Thank you for your help.

  • 1
    Right now your `array_point_power` is not compare power. You just assign the score in order. If you want to keep `array_point_power` like that, you need to order ascending your players by power and after execute `array_point_power` – KunLun Jul 01 '20 at 08:54
  • @KunLun: Thank you for your help, but how to compare the array array_power_1 and array_point_power() in my array_point_power() method? I don't understand. –  Jul 01 '20 at 09:16
  • 1
    I'm talking about compare power of players from `array_Player` in method `array_point_power`. You need to find the lower, the next one after lower and so on, to know how to assign the score. – KunLun Jul 01 '20 at 09:33

3 Answers3

0

In the array_point_power you never compare the players power you just assign the score in order.

You will have to sort the players by their power and then award the score.
For this it would be best to create a Player object with their name and power and make it sortable

Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
0

A more java way to do this is to use encapsulation and make a Player class

public class Player {

   private String name;
   private int power;
   private int points;

   public Player(String name, int power) {
       this.name = name;
       this.power = power;
   }

   public String getName() {
       return name;
   }

   public int getPower() {
       return power;
   }

   public void setPoints(int points) {
       this.points = points;
   }

   public int getPoints() {
       return points;
   }
}

Then in your main class

public class Main {


   public static void main(String[] args) {

       List<Player> players = new ArrayList<>();
       players.add(new Player("Celine", 4));
       players.add(new Player("Amelia", 2));
       players.add(new Player("Olivia", 10));

       bubbleSort(players);
       array_point_power(players);

       System.out.println("Round 1 : ");
       showResults(players);
   }

   //If the points is based on multiples of 2 you can use i to assign points
   private static void array_point_power(List<Player> players) {
       for (int i = 0; i < players.size(); i++) {
           players.get(i).setPoints(i * 2 + 2);
       }
   }

   //Make a simple bubble sort based on the players powers
   private static void bubbleSort(List<Player> players) {
       for (int i = 0; i < players.size(); i++) {
           for (int j = 0; j < players.size(); j++) {
               if (players.get(i).getPower() < players.get(j).getPower()) {
                   Player temp = players.get(i);
                   players.set(i, players.get(j));
                   players.set(j, temp);
               }
           }
       }
   }

   private static void showResults(List<Player> players) {
       for (int i = 0; i < players.size(); i++) {
           System.out.println("Joueur " + players.get(i).getName() + 
                              " | Puissances " + players.get(i).getPower() + 
                              " | Points " + players.get(i).getPoints());
       }
   }
}
  • Thank you very much for your help. I am beginner, we only learn the procedural programming and not Object Oriented Programming. I am lost in your code. :-( –  Jul 01 '20 at 09:42
0
class Main {
   public static void main(String[] args) {

       String[] playersArray = {"Celine", "Amelia", "Olivia"};
       int[] powerArray = {4, 2, 10};
    
       //unsorted array of power
       int[] powerIndexes = {0, 1, 2};
       int[] pointsArray = new int[3];
    
       powerIndexes(powerArray, powerIndexes);
       assignPoints(powerIndexes, pointsArray);
       System.out.println("Round 1 : ");
       showResults(playersArray, powerArray, pointsArray);

   }

   //Change power indexes based on power
   public static void powerIndexes(int[] powerArray, int[] powerIndexes) {
       for (int i = 0; i < powerArray.length; i++) {
           for (int j = 0; j < powerArray.length; j++) {
               if (powerArray[i] < powerArray[j]) {
                   int temp = powerIndexes[i];
                   powerIndexes[i] = powerIndexes[j];
                   powerIndexes[j] = temp;
               }
           }
       }
   }

   public static void assignPoints(int[] powerIndexes, int[] pointsArray) {
       for (int i = 0; i < powerIndexes.length; i++) {
           pointsArray[i] = powerIndexes[i] * 2 + 2;
       }
   }

   public static void showResults(String[] playersArray, int[] powerArray, int[] pointsArray) {

       for (int i = 0; i < playersArray.length; i++) {
           System.out.println("Joueur " + playersArray[i] + " | Puissances " + powerArray[i] + " | Points " + pointsArray[i]);
    }

   }
}
  • Thank you, thank you a lot Voinea Radu to have solve my problem. I was block for 3 days. Thank you very much. –  Jul 01 '20 at 10:49