In an array [] I have to extract the maximum and if another position is also maximum concatenate it

0

1.- The following countries participate in a championship: "Spain", "France", "United Kingdom", "Italy", "Germany" and "Denmark". Each country gets a score (between 1 and 10 that is generated randomly). It is requested to calculate the countries that have obtained the first and second prize. (There may be several countries with the same score). It is asked to solve the problem using arrays.

Example: assuming that the order of the countries is the previous one and that the scores are respectively 8, 9, 5, 6, 7, 3 the output would be: Winner of the first prize: France Second prize: Spain

package arrays;

public class Paises {

    public static void main(String[] args) {
        String[] listaPaises= {"España", "Francia", "Reino Unido", "Italia", "Alemania","Dinamarca"};
        int max =Integer.MIN_VALUE,posPai = -1;
        for (int i = 0; i < listaPaises.length; i++) {
            int puntuaciones = (int)(Math.random()*9+1);
            System.out.print(puntuaciones+" ");
            if (puntuaciones>max) {
                max=puntuaciones;
                 posPai = i;
                 listaPaises[posPai]+=" "+listaPaises[i];
            }
            //listaPaises[i]
        } 
        System.out.println(" "+max);
        System.out.println("El ganador es "+listaPaises[posPai]);
    }

}

I've tried it in several ways but I do not give it with the key.

    
asked by David Palanco 31.05.2018 в 01:14
source

2 answers

1

What you should do is check if the two scores are the same and if they are concatenated, something like this:

package arrays;

public class Paises {

    public static void main(String[] args) {
        String[] listaPaises= {"España", "Francia", "Reino Unido", "Italia", "Alemania","Dinamarca"};
        int max =Integer.MIN_VALUE,posPai = -1;
        for (int i = 0; i < listaPaises.length; i++) {
            int puntuaciones = (int)(Math.random()*9+1);
            System.out.print(puntuaciones+" ");
            if (puntuaciones>max) {
                max=puntuaciones;
                 posPai = i;
                 listaPaises[posPai]=listaPaises[i];
            } else if (puntuaciones == max) {
                 listaPaises[posPai]+=" "+listaPaises[i]; 
            }
            //listaPaises[i]
        } 
        System.out.println(" "+max);
        System.out.println("El ganador es "+listaPaises[posPai]);
    }

}
    
answered by 31.05.2018 / 01:44
source
1

To show the first and second team with more points you need two different variables to save the first and second separately as you go through the for. Similar to your code but with two max and two posPai. So:

public class Paises {

    public static void main(String[] args) {
        String[] listaPaises = { "España", "Francia", "Reino Unido", "Italia", "Alemania", "Dinamarca" };
        int max1 = Integer.MIN_VALUE;
        int max2 = Integer.MIN_VALUE;
        int posPai1 = -1;
        int posPai2 = -1;

        for (int i = 0; i < listaPaises.length; i++) {
            int puntuaciones = (int) (Math.random() * 9 + 1);
            System.out.print(puntuaciones + " ");

            // Si es mayor o = que el 1º (y por lo tanto también mayor que el 2º)
            // Pasamos el 1º al 2º y actualizamos el nuevo 1º
            if (puntuaciones >= max1) {
                max2 = max1;
                max1 = puntuaciones;
                posPai2 = posPai1;
                posPai1 = i;
            }
            // Si es mayor o igual que el 2º (pero no mayor o = que el 1º)
            // Actualizamos el nuevo 2º
            else if (puntuaciones >= max2) {
                max2 = puntuaciones;
                posPai2 = i;
            }
        }

        System.out.println(" Max: " + max1 + " " + max2);
        System.out.println("Ganador del primer premio: " + listaPaises[posPai1] + " Segundo premio: " + listaPaises[posPai2] );
    }

}

EDIT: The statement says that there may be several countries with the same score, but it does not make clear what to do in such a case. For example if we have 9 9 8 8 7 7 Would the two 9 be first and second place randomly? Or would we have two first places with 9 and two second places with 8? What if there are three with the same number?

The simplest thing would be that there could be several teams with the same position, but the statement does not make it clear.

    
answered by 31.05.2018 в 01:54