Use of repeat loop appropriately

Asked

Viewed 64 times

1

"A very common joke among 3 friends is the game of 2 or 1. In this game, the winner is different from the other two and, if the three choose equal values, the game ends tied. So consider three friends A, B and C, who are playing 2 or 1: your show should show which one is the winner or if there was a tie. For this, read an N number that indicates the total number of matches 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 draw. The program will end when N is zero.

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 it’s validating who the winner is and it’s a draw. The problem is the amount I report, and it shows only once, and when I enter 0 it does not go straight to "Game Over!".

1 answer

2


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

One of the mistakes is that it asks how many games you will play every time you make a match.

Another error is that you are not decreasing the number of missing moves.

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

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

This way it 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!");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The other variables could still have better names.

I’m trusting the general logic to be correct.

  • Thank you very much friend! I was even breaking my head with the do...while and it was the detail of the decrease. I also appreciate the explanation of the break within the loop. Good evening!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.