0

I have a problem that I can not solve with a java loop for a class exercise. I need to make a program that asks 10 questions of random multiplication tables. At the end of the 10 questions, it has to show me the questions that were right the first time. In case a question is not right the first time, it must show the table of the failed number and then reformulate the question.

The problem I have is that within the for of the 10 questions I have an if -else for the right questions and inside the else the do-while to reformulate the questions. The problem is when I fail the question, the program reformulates it as it should do but if I enter the correct answer, the do-while loop closes the for and stops asking questions.

The use of arrays is banned in the exercise.

Here is the code:

for (int i=1; i<=10; i++) {
    Tablas operacion=new Tablas();

    int pregunta = Integer.parseInt(JOptionPane.showInputDialog(null, operacion.getNumeroPregunta()+"\n"+operacion.multiplicacion()));
    if (pregunta == operacion.resultado()) {
        Tablas.comprobadorPreguntas(true);
    }
    else {
        do {
        String salida="";
        for (i=1; i<=10; i++) {
            salida+=operacion.getMultiplicando() + "x"+i+"=" + (operacion.getMultiplicando() * i) + "\n";
        }
        JOptionPane.showMessageDialog(null, salida);
        pregunta = Integer.parseInt(JOptionPane.showInputDialog(null, operacion.getNumeroPregunta()+"\n"+operacion.multiplicacion()));
        } while(pregunta != operacion.resultado());
        Tablas.comprobadorPreguntas(false);
    }
}

JOptionPane.showMessageDialog( null, "Preguntas acertadas a la primera: "+Tablas.getContador());

Here are also the classes in pastebin:

Program > https://pastebin.com/imAQBuRX

Methods > https://pastebin.com/GJdyrnRv

1 Answer 1

1

It looks like you use i in both of your loops. When the do-while loop executes, it ends with i being 10, so when the for loop condition is checked, i=10 and the for loop finishes. Try using another variable - using j in the inner loop is traditional - to avoid this.

For example, here is a nested set of for loops with proper variable names:

for(int i = 0; i < 10; i++){
    for(int j = 0; j < 10; j++;){
       ...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Its works fine now. Thanks you very much for your time. Regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.