Why does not the if function?

0

Enter the result of 4 matches played between team A and team B. Determine and report the number of matches won by each and the winner of more matches if they did not get the same results.

This time my problem is that if I enter the same number of matches won (for both teams) it tells me that Team B is the one with the most amount of teams won, when I would have to say a draw.

    int partidos = 5;
    int equipoA = 0;
    int equipoB = 0;
    int partidosGanados1 = 0;
    int partidosGanados2 = 0;

    Scanner teclado = new Scanner(System.in);

    for (int i = 1; i < partidos; i++) {

        System.out.println(i + "Ingrese goles del equipo A : ");
        equipoA = teclado.nextInt();
        System.out.println(i + "Ingrese goles del equipo B : ");
        equipoB = teclado.nextInt();

        if (equipoA > equipoB) {

            partidosGanados1++;
            System.out.println("Gana equipo A .");
        }

        if (equipoB > equipoA) {

            partidosGanados2++;

            System.out.println("Gana equipo B .");

        }

    }

    if (equipoA != equipoB) {

        if (equipoA > equipoB)

        {

            System.out.println("El ganador de mas partidos es el equipo A. ");
        }

        else {

            System.out.println("El ganador de mas partidos es el equipo B. ");
        }

        System.out.println("No hay empate.");
    }

    else if (equipoA == equipoB ) {

        System.out.println("Empate.");
    }

    System.out.println("Cantidad de partidos ganados por el equipo A : " + partidosGanados1);
    System.out.println("Cantidad de partidos ganados por el equipo B : " + partidosGanados2);
    
asked by computer96 27.08.2018 в 02:20
source

2 answers

2

The problem was that you did not compare the variables that were storing the results for each cycle:

Try this way:

int partidos = 5;
int equipoA = 0;
int equipoB = 0;
int partidosGanados1 = 0;
int partidosGanados2 = 0;

Scanner teclado = new Scanner(System.in);

for (int i = 1; i < partidos; i++) {

    System.out.println(i + "Ingrese goles del equipo A : ");
    equipoA = teclado.nextInt();
    System.out.println(i + "Ingrese goles del equipo B : ");
    equipoB = teclado.nextInt();

    if (equipoA > equipoB) {

        partidosGanados1++;
        System.out.println("Gana equipo A .");
    }
    else if (equipoB > equipoA) {

        partidosGanados2++;
        System.out.println("Gana equipo B .");
    }
    else{
        System.out.println("Hubo Empate .");
    }

}

if (partidosGanados1 != partidosGanados2) {

    if (partidosGanados1 > partidosGanados2)

    {

        System.out.println("El ganador de mas partidos es el equipo A. ");
    }

    else {

        System.out.println("El ganador de mas partidos es el equipo B. ");
    }

    System.out.println("No hay empate.");
}
else{

    System.out.println("Empate.");
}

System.out.println("Cantidad de partidos ganados por el equipo A : " + partidosGanados1);
System.out.println("Cantidad de partidos ganados por el equipo B : " + partidosGanados2);
    
answered by 27.08.2018 / 02:33
source
1

Friend the flaw is when comparing the variables, instead of comparing the variables of matchesGanados1 and matchesGanados2, you are comparing teamA and teamB. I would also like to give you a couple of tips about your code that may benefit you to reduce the number of lines you write and help you understand a little better if / else.

Check it and I hope you serve, greetings.

int partidos = 5;
int equipoA = 0;
int equipoB = 0;
int partidosGanados1 = 0;
int partidosGanados2 = 0;

Scanner teclado = new Scanner(System.in);

for (int i = 1; i < partidos; i++) {

    System.out.println(i + "Ingrese goles del equipo A : ");
    equipoA = teclado.nextInt();
    System.out.println(i + "Ingrese goles del equipo B : ");
    equipoB = teclado.nextInt();

    if (equipoA > equipoB) 
    {

        partidosGanados1++;
        System.out.println("Gana equipo A .");
    }

    else if(equipoA<equipoB)
    {

        partidosGanados2++;

        System.out.println("Gana equipo B .");

    }
    else
    {
        System.out.println("Es empate");
    }

}
 if (partidosGanados1 > partidosGanados2)
 {
      System.out.println("El ganador de mas partidos es el equipo A. ");
      System.out.println("No hay empate.");
 }

 else if(partidosGanados1 < partidosGanados2)
 {

     System.out.println("El ganador de mas partidos es el equipo B. ");
     System.out.println("No hay empate.");
 }
 else
 {
     System.out.println("Hay empate");
 }

System.out.println("Cantidad de partidos ganados por el equipo A : " + partidosGanados1);
System.out.println("Cantidad de partidos ganados por el equipo B : " + partidosGanados2);
    
answered by 27.08.2018 в 04:25