Code structure problem within switch case

Asked

Viewed 793 times

0

When choosing option 1 (easy) the program enters case 1 correctly, but when I choose the port (0, 1 or 2) it goes back to the menu, the goal is that if the port is the right one it passes to the next line with the new sequence of ports (second line of the matrix) and if it is not the right 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);
        }
    }
}
  • Thanks @mgibsonbr, could you point me the path of light to make it less "noodle"? hehehe. Just a tip even. Thank you.

  • I switched to go, it was like this: I still have to cut but it’s almost there.

  • for(i =0; i<port.length;i++){ System.out.println("Level "+i); for(j=0; j<porta.length;j++){ System.out.println ("Elige una Puerta: Tenemos las Puertas 0,1,2"); String tentaStr = in.readline (); tries = Integer.parseint(tentaStr); } if(port[i][tries] !=1){continue;} }

1 answer

2


All your code is contained within a loop that tests through an input invalid:

// 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 with an invalid entry on his first attempt, he will not even enter the game...

To resolve, you need to close this loop that repeats the entry in the invalid case, and only enter the game itself when the entry 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, because there is loop inside loop inside loop, and everything in a function only... I suggest breaking it into smaller functions, for better understanding and maintainability (I for example 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 you continue (i.e. 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 there were 50 levels? Or if the number of levels was 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), that even works in the case of the boolean from Java, but it wouldn’t be better to use a boolean OR? tenta<0 || tenta>2

  • Thank you so much!! I am learning this part of the functions now but I will try to do as you said.

Browser other questions tagged

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