Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
Why isn't my output showing after my switch statement

I'm learning Java right now and I've never used switch statements before. I tried to enter a simple charmed quiz, but something in the switch statement isn't working.

I've tried putting text at various points in the program to test if the program every reaches that code. I have a good response inside the actual switch, so If I answer Question 1 wrong the text prompt will show up. But any later than inside the switch statement and none of my scoring output appears until all iterations of the for loop are complete. I have tried moving the "correct/incorrect" output to various points and none of them seem to work.

Scanner myScanner = new Scanner(System.in);

System.out.println("Enter your name!");
String name = myScanner.nextLine();
int wrongCounter = 0;
boolean correctChecker = false;
int score = 0;
String answer;

System.out.println("Welcome to the Charmed Quiz, " + name + "!");

for (int i = 0; i < 10; i++) {
    if (wrongCounter < 4) {
        switch(i) {
        case 0: 
            System.out.println("Who read the spell that gave the Charmed Ones their powers?");
            System.out.println("Enter your answer");

            answer = myScanner.nextLine();

            switch (answer) {
            case "Pheobe":
                correctChecker = true;
                break;
            default:
                correctChecker = false;
                break;
            }
        case 1:
            System.out
                    .println("Who travelled to a cursed town with Prue when Pheobe was shot in a premonition?");
            System.out.println("Enter your answer");

            answer = myScanner.nextLine();

            switch (answer) {
            case "Cole":
                correctChecker = true;
                break;
            default:
                correctChecker = false;
                break;
            }
        }
        
    

    if (correctChecker == true) {
        score++;
        System.out.println("Correct!");
    } else {
        wrongCounter++;
        System.out.println("Incorrect!");

    }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

There are many, many problems with this code. The primary issue is that break breaks the closest construct it can break, which in your case is the inner switch. Whereas your intent is clearly to break out of both. Either [A] add another break right before the case 1: statement, or [B] use a labelled break; put something like outer: before the first (primary/outer) switch, and then make all those statements break outer;.

But, really, none of this (either the outer or the inner) are in any way sensible in switch form. I get that this is a learning exercise, but I'd think of something else to learn with.

Also, it's Phoebe, not Pheobe.

over 4 years ago · Santiago Trujillo Denunciar

0

This definitely isn't the best way of achieving a quiz game, but if you're using this as a learning exercise then the best course of action is to take the advice from @rzwitserloot.

Add a break after your main switch statement cases as opposed to the inner switch statement.

There is no real use having an inner switch statement though when you can use correctChecker = "Pheobe".equals(answer); to get a true or false boolean value in a single line.

This just means you can avoid the second switch statement which makes it way less confusing.

Altogether your cases could look something like this:

    case 0: 
        System.out.println("Who read the spell that gave the Charmed Ones their powers?");
        System.out.println("Enter your answer");

        answer = myScanner.nextLine();
        correctChecker = "Pheobe".equals(answer);
        break;
        
        }

In future, it would be better to store questions and answers in an array and use the for loop to iterate through that. This is a good tutorial on the subject.

Good luck with the rest of your project!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda