mardi 30 mars 2021

Two if statements in a switch in Java. Only one If works when approached

I am pretty new to development and I have a question. I have a program and I want two conditions with if statements. b, t and c, t. Case 1 works, Case two only the part up to sm(); works

System.out.println("lots of text.");
        System.out.println("1. I want x");
        System.out.println("2. I want z");
        choice = myScanner.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("lots of text");
                        k(); 
                        break;
                case 2:
                    System.out.println ("lots of text3");
                        if (b == 1){
                        System.out.println ("lots of text4");
                        t=1;
                        b--;
                         System.out.println("1: go");
                      choice = myScanner.nextInt();
                      sm();
                        }
                        if ( c == 1){
                        System.out.println ("lots of text5");
                        t=1;
                 choice = myScanner.nextInt();
                System.out.println("1: go ");
                    sm(); 
                    break;
                }
             }
        }
}

k and sm are public voids in the program. The program should jump to either one of them them if either b=1 or c=1. Never will both equal 1. Only one of them should get triggered when approaching this part of the code. However, the problem is if(c==1) is never getting triggered. The program just stops at System.out.println ("lots of text3"); It does though work perfectly fine when b==1. Then the program goes to sm();

Any ideas? I checked and c will equal 1 prior to approaching this part of the code.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire