• Register

Solution:

Run this below code that should solve your problem

Code:

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]);
    }

   }
}

Or, applying encapsulation and make a player classic

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;
   }
}

Now, you have to execute below code 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());
       }
   }
}

Hopefully, You are able to solve your problem.

posted Jul 1 in java 17,260 points