Change the array of strings to an int

0

What I want in this program is that instead of making a comparison with the equals use (=) with an array of booleans.

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

    String[] equipos = new String[] {"REAL MADRID"," MILAN", "BAYERN"," BARCELONA"," MANCHESTER UNITED"}; 
    int[] champions=new int [] {12,7,5,5,3};
    String[] repetidos = new String[5];
    char opcion='S';
    int intentos=0;
    int encertados=0;
    int posequipo=0;
    String selecionado="";
    int cantidadChampions=0;
    int poschampions=0;
    int numequipos=0;
    Random genEquipos= new Random();
    boolean correcto= false;

    System.out.println("Puntos: "+encertados+" encertados en "+intentos+" intentos");
    for (int i = 0; i < repetidos.length; i++) {
        repetidos[i]="";
    }
    do{         
        posequipo=genEquipos.nextInt(5);
        while(true){
            if (!equipos[posequipo].equals(repetidos[posequipo])){
                selecionado = equipos[posequipo];
                numequipos++;
                repetidos[posequipo]=selecionado;
                break;
            }else{
                posequipo = genEquipos.nextInt(5);
                selecionado = equipos[posequipo];
            }

        }

        System.out.println("Numero de Champions del equipo "+selecionado);
        cantidadChampions=lector.nextInt();
        correcto=false;

        for (int j = 0; j < champions.length; j++) {
            poschampions=posequipo;         

            if (cantidadChampions==champions[poschampions]){
                encertados++;
                intentos++;
                correcto= true;
                break;

        }else{
            correcto=false;
            intentos++;
            break;
        }
        }       
        if(correcto==true){
            System.out.println("Felicidades Maquina!!");
            System.out.println("Puntos: "+encertados+" encertados en "+intentos+" intentos");
        }else{
            System.out.println("Me has decepcionado");
            System.out.println("Puntos: "+encertados+" encertados en "+intentos+" intentos");
        }
        if(intentos==5){
        System.out.println("Puntacion final: "+encertados+" encertados en "+intentos+" intentos");
        System.out.println("Quieres jugar otra vez? (S/N)");
        opcion=lector.next().charAt(0);
        }   

    } while(opcion =='S'||opcion =='s' );

    System.out.println("Se acabo el juego");


}
}
    
asked by GotenkSSS7 01.03.2018 в 11:11
source

1 answer

1

You can do an enumeration, so the comparison between two strings becomes a comparison between two int. You can not pass it to an array of booleans, but with enums it becomes an array of integers and you can use the operator (=) instead of equals.

I'll give you an example with your code:

public enum Equipos{NINGUNO, REAL_MADRID, MILAN, BAYERN, BARCELONA, MANCHESTER_UNITED};

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

    Equipos[] equipos = new Equipos[] {Equipos.REAL_MADRID, Equipos.MILAN, Equipos.BAYERN, Equipos.BARCELONA, Equipos.MANCHESTER_UNITED}; 
    int[] champions=new int [] {12,7,5,5,3};
    Equipos[] repetidos = new Equipos[5];
    char opcion='S';
    int intentos=0;
    int encertados=0;
    int posequipo=0;
    Equipos selecionado= Equipos.NINGUNO;
    int cantidadChampions=0;
    int poschampions=0;
    int numequipos=0;
    Random genEquipos= new Random();
    boolean correcto= false;

    System.out.println("Puntos: "+encertados+" encertados en "+intentos+" intentos");
    for (int i = 0; i < repetidos.length; i++) {
        repetidos[i]=Equipos.NINGUNO;
    }
    do{         
        posequipo=genEquipos.nextInt(5);
        while(true){
            if (equipos[posequipo] != repetidos[posequipo]){
                selecionado = equipos[posequipo];
                numequipos++;
                repetidos[posequipo]=selecionado;
                break;
            }else{
                posequipo = genEquipos.nextInt(5);
                selecionado = equipos[posequipo];
            }

        }

        System.out.println("Numero de Champions del equipo "+selecionado);
        cantidadChampions=lector.nextInt();
        correcto=false;

        for (int j = 0; j < champions.length; j++) {
            poschampions=posequipo;         

            if (cantidadChampions==champions[poschampions]){
                encertados++;
                intentos++;
                correcto= true;
                break;

        }else{
            correcto=false;
            intentos++;
            break;
        }
        }       
        if(correcto==true){
            System.out.println("Felicidades Maquina!!");
            System.out.println("Puntos: "+encertados+" encertados en "+intentos+" intentos");
        }else{
            System.out.println("Me has decepcionado");
            System.out.println("Puntos: "+encertados+" encertados en "+intentos+" intentos");
        }
        if(intentos==5){
        System.out.println("Puntacion final: "+encertados+" encertados en "+intentos+" intentos");
        System.out.println("Quieres jugar otra vez? (S/N)");
        opcion=lector.next().charAt(0);
        }   

    } while(opcion =='S'||opcion =='s' );

    System.out.println("Se acabo el juego");


}
    
answered by 01.03.2018 / 11:58
source