Use of repetition loop appropriately

1
  

"A very common game between 3 friends is the game of 2 or 1. In this game, the winner is different from the other two, and if all three choose equal values, the game ends in a tie. , B and C, who are playing 2 or 1: their program must show which of them is the winner or if there was a draw, read a number N that indicates the total number of games they will play and then read the values A, B, and C (integers) of each friend, consider that these values will always be 2 or 1. Show on the screen who was the winner or if there was a tie.

My code:

public static void main(String[] args) {
    Scanner ler = new Scanner(System.in);
    Random rand = new Random();
    int numero1 = rand.nextInt(2) + 1;
    int numero2 = rand.nextInt(2) + 1;
    int numero3 = rand.nextInt(2) + 1;
    int N;
    System.out.println("DOIS OU UM!");
    System.out.print("Quantas partidas serão jogadas? ");
    do {
        N = ler.nextInt();
        System.out.println("Jogador 1: "+numero1);
        System.out.println("Jogador 2: "+numero2);
        System.out.println("Jogador 3: "+numero3);
        System.out.println("------------");
        if (numero1 != numero2 && numero1 != numero3)
            System.out.println("Jogador 1 venceu!");
        else if (numero2 != numero1 && numero2 != numero3)
            System.out.println("Jogador 2 venceu!");
        else if (numero3 != numero1 && numero3 != numero2)
            System.out.println("Jogador 3 venceu!");
        else
            System.out.println("O jogo terminou empatado!");
        break;

    } while (N > 0);

    System.out.println("Fim de Jogo!");

}

The code until you are validating who is the winner and if there is a tie. The problem is in the amount I report, and it shows only once, and when I enter 0 it does not go straight to the "Game End!".

    
asked by anonymous 29.03.2018 / 02:22

1 answer

2

If you read the statement carefully you will see that the code does not do the same thing that is written there.

One of the mistakes is that you ask how many matches you play each time you play.

Another error is that it is not decreasing the number of missing plays.

A break just before the end of the loop is the same as nothing.

Even if it were to still ask if you want to play you would have to instruct the computer to leave immediately, it does not magically leave because someone typed some number. The computer needs to be instructed in every detail.

This works and becomes more readable:

public static void main(String[] args) {
    Random rand = new Random();
    int numero1 = rand.nextInt(2) + 1;
    int numero2 = rand.nextInt(2) + 1;
    int numero3 = rand.nextInt(2) + 1;
    System.out.println("DOIS OU UM!");
    System.out.print("Quantas partidas serão jogadas? ");
    Scanner ler = new Scanner(System.in);
    int numPartidas = ler.nextInt();
    do {
        System.out.println("Jogador 1: " + numero1);
        System.out.println("Jogador 2: " + numero2);
        System.out.println("Jogador 3: " + numero3);
        System.out.println("------------");
        if (numero1 != numero2 && numero1 != numero3) System.out.println("Jogador 1 venceu!");
        else if (numero2 != numero1 && numero2 != numero3) System.out.println("Jogador 2 venceu!");
        else if (numero3 != numero1 && numero3 != numero2) System.out.println("Jogador 3 venceu!");
        else System.out.println("O jogo terminou empatado!");
    } while (--numPartidas > 0);
    System.out.println("Fim de Jogo!");
}

The other variables could still have better names.

I'm relying on general logic to be correct.

    
29.03.2018 / 02:34