Problem with the code structure inside the switch case

0

When choosing option 1 (easy) the program enters case 1 correctly, but when I choose the port (0, 1 or 2) it returns to the menu, the goal is that if the port is right, the next line with the new port sequence (second line of the array) and if it is not the correct port the user should try again. Thank you.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class Ejerc9 {
    public static void main(String[] args) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            int opcion = 0;
            int tenta =0;
            int i=0;
            int j=0;
            int [][] porta = new int [3][3];
            Random r = new Random();
            // para cada linha
            int qual=0;
            int cont1=0;
            int cont2=0;
            int count3=0;

            while (opcion != 4) {
                System.out.println ("Juego de las 3 Puertas, intente acertar la puerta buena");
                System.out.println ("Elige la dificuldad:");
                System.out.println ();
                System.out.println ("1.Facil");
                System.out.println ("2. Medio");
                System.out.println ("3. Avanzado");
                System.out.println ("4. Salir");
                System.out.println ();

                String opcionStr = in.readLine ();
                opcion = Integer.parseInt(opcionStr);

                while (opcion < 1 || opcion > 4) {
                    System.out.println ("Elige la dificuldad (1,2,3):");
                    System.out.println ();
                    System.out.println ("1.Facil");
                    System.out.println ("2. Medio");
                    System.out.println ("3. Avanzado");
                    System.out.println ("4. Salir");
                    System.out.println ();

                    opcionStr = in.readLine ();
                    opcion = Integer.parseInt(opcionStr);
                }

                switch (opcion) {
                  case 1:
                    qual = r.nextInt(3);
                    for(i =0; i<3;i++){
                        qual = r.nextInt(3);
                        porta[i][qual] = 1;
                        for(j=0; j<3;j++){
                        }
                    }

                    System.out.println ("Elige una Puerta: Tenemos las puertas 0, 1 y 2");
                    String tentaStr = in.readLine ();
                    tenta = Integer.parseInt(tentaStr);
                    while(tenta<0|tenta>2){
                        System.out.println ("Elige una Puerta valida: Tenemos las puertas 0, 1 y 2");
                        tentaStr = in.readLine ();
                        tenta = Integer.parseInt(tentaStr);

                        while(tenta!=porta[0][qual]){

                            System.out.println ("Elige otra Puerta");

                            tentaStr = in.readLine ();
                            tenta = Integer.parseInt(tentaStr);
                        }
                        if(tenta==porta[0][qual]){
                            i++;
                            cont1=cont1+1;
                            System.out.println (" Nivel 2 -Elige una Puerta : Tenemos las puertas 0, 1 y 2");
                            tentaStr = in.readLine ();
                            int tenta2 = Integer.parseInt(tentaStr);
                            while(tenta2<0|tenta2>2){
                                System.out.println ("Elige una Puerta valida: Tenemos las puertas 0, 1 y 2");
                                tentaStr = in.readLine ();
                            tenta2 = Integer.parseInt(tentaStr);}
                            while(tenta2!=porta[1][qual]){

                                System.out.println ("Elige otra Puerta");

                                tentaStr = in.readLine ();
                                tenta2 = Integer.parseInt(tentaStr);
                            }

                            if(tenta2==porta[1][qual]){
                                i++;
                                cont1=cont1+1;

                                System.out.println (" Nivel 3 -Elige una Puerta : Tenemos las puertas 0, 1 y 2");
                                tentaStr = in.readLine ();
                                int tenta3 = Integer.parseInt(tentaStr);
                                while(tenta3<0|tenta3>2){
                                    System.out.println ("Elige una Puerta valida: Tenemos las puertas 0, 1 y 2");
                                    tentaStr = in.readLine ();
                                tenta3 = Integer.parseInt(tentaStr);}
                                while(tenta3!=porta[2][qual]){

                                    System.out.println ("Elige otra Puerta");

                                    tentaStr = in.readLine ();
                                    tenta3 = Integer.parseInt(tentaStr);
                                }
                                if(tenta3==porta[2][qual]){
                                    i++;
                                cont1=cont1+1;}
                            }
                        }
                    }
                    if(cont1==3){
                        System.out.println ("Congrats!!!");
                    }
                    break;
                  case 2:
                    break;
                  case 3:
                    break;
                  case 4:
                    System.out.println("Hasta luego.");
                    break;
                }
            }
        }
        catch(Exception e){
            System.out.println("Error 400" +e);
        }
    }
}
    
asked by anonymous 26.04.2015 / 13:59

1 answer

2

All your code is contained within a loop that tests for an invalid entry:

// Pede uma porta
System.out.println ("Elige una Puerta: Tenemos las puertas 0, 1 y 2");
String tentaStr = in.readLine ();
tenta = Integer.parseInt(tentaStr);

// Se a porta for INVÁLIDA, repete esse loop
while(tenta<0|tenta>2){
    // Pede outra porta
    System.out.println ("Elige una Puerta valida: Tenemos las puertas 0, 1 y 2");
    tentaStr = in.readLine ();
    tenta = Integer.parseInt(tentaStr);

    // NÃO testa se a nova entrada foi válida!!!
    while(tenta!=porta[0][qual]){ // Entre no jogo
        ...

That is, unless the user enters an invalid entry on his first attempt, he will not even enter the game ...

To resolve, you need to terminate this loop that repeats the input in the invalid case, and only enter the game itself when the input is valid :

// Pede uma porta
System.out.println ("Elige una Puerta: Tenemos las puertas 0, 1 y 2");
String tentaStr = in.readLine ();
tenta = Integer.parseInt(tentaStr);

// Se a porta for INVÁLIDA, repete esse loop
while(tenta<0|tenta>2){
    // Pede outra porta
    System.out.println ("Elige una Puerta valida: Tenemos las puertas 0, 1 y 2");
    tentaStr = in.readLine ();
    tenta = Integer.parseInt(tentaStr);
}

// Agora temos certeza que a entrada é válida (pois saiu do loop de cima)
while(tenta!=porta[0][qual]){ // Entre no jogo
    ...

P.S. As it is, it is very difficult to understand what the code is doing, since there is a loop inside a loop inside the loop, and everything in a single function ... I suggest breaking it into smaller functions, for better understanding and maintainability example I could only see the problem in fact after I started this process):

public class Ejerc9 {
    public static void main(String[] args) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            int opcion = 0;
            Random r = new Random();

            while (opcion != 4) {
                System.out.println ("Juego de las 3 Puertas, intente acertar la puerta buena");
                opcion = dificuldad("");

                while (opcion < 1 || opcion > 4) {
                    opcion = dificuldad(" (1,2,3)");
                }

                switch (opcion) {
                  case 1:
                    dif1(in, r);
                    break;
                  case 2:
                    break;
                  case 3:
                    break;
                  case 4:
                    System.out.println("Hasta luego.");
                    break;
                }
            }
        }
        catch(Exception e){
            System.out.println("Error 400" +e);
        }
    }

    private static int dificuldad(String etc) {
        System.out.println ("Elige la dificuldad" + etc + ":");
        System.out.println ();
        System.out.println ("1.Facil");
        System.out.println ("2. Medio");
        System.out.println ("3. Avanzado");
        System.out.println ("4. Salir");
        System.out.println ();

        String opcionStr = in.readLine ();
        return Integer.parseInt(opcionStr);
    }

    private static void dif1(BufferedReader in, Random r) {
        int tenta =0;
        int i=0;
        int j=0;
        int [][] porta = new int [3][3];
        // para cada linha
        int cont1=0;
        int cont2=0;
        int count3=0;

        criaPorta(porta, r);

        System.out.println ("Elige una Puerta: Tenemos las puertas 0, 1 y 2");
        tenta = escolhePorta(in);

        while(tenta!=porta[0][qual]){
            System.out.println ("Elige otra Puerta");
            tenta = escolhePorta(in);
        }

        if(tenta==porta[0][qual]){
            i++;
            cont1=cont1+1;
            System.out.println (" Nivel 2 -Elige una Puerta : Tenemos las puertas 0, 1 y 2");
            int tenta2 = escolhePorta(in);
            while(tenta2!=porta[1][qual]){

                System.out.println ("Elige otra Puerta");
                tenta2 = escolhePorta(in);
            }

            if(tenta2==porta[1][qual]){
                i++;
                cont1=cont1+1;

                System.out.println (" Nivel 3 -Elige una Puerta : Tenemos las puertas 0, 1 y 2");
                int tenta3 = escolhePorta(in);
                while(tenta3!=porta[2][qual]){

                    System.out.println ("Elige otra Puerta");
                    tenta3 = escolhePorta(in);
                }
                if(tenta3==porta[2][qual]){
                    i++;
                    cont1=cont1+1;
                }
            }
        }
        if(cont1==3){
            System.out.println ("Congrats!!!");
        }
    }

    private static void criaPorta(int[][] porta, Random r) {
        int qual = r.nextInt(3);
        for(i =0; i<3;i++){
            qual = r.nextInt(3);
            porta[i][qual] = 1;
            for(j=0; j<3;j++){
            }
        }
    }

    private static int escolherPorta(BufferedReader in) {
        String tentaStr = in.readLine ();
        int tenta = Integer.parseInt(tentaStr);
        while(tenta<0|tenta>2){
            System.out.println ("Elige una Puerta valida: Tenemos las puertas 0, 1 y 2");
            tentaStr = in.readLine ();
            tenta = Integer.parseInt(tentaStr);
        }
        return tenta;
    }
}

I stopped as soon as I identified the problem, but I suggest that it continue (i.e. by identifying common snippets and putting in functions). I see for example that each level is contained within the previous level (i.e. loop within loop). Challenge: how would you do if instead of 3 were 50 levels? Or if the number of levels were informed by the user? There are at least two ways to do this, so iterative or recursive ...: P

P.P.S. tenta<0|tenta>2 is a binary OR ( bitwise ), which even works in the case of type boolean of Java, but would not it be better to use a Boolean OU? tenta<0 || tenta>2

    
27.04.2015 / 20:05