Problem with do while loop inside a for

-1

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, you have to show me the questions that were right the first time. In case a question is not answered to the first one, it should show the table of the failed number and then reformulate the question.

The mistake I have is that within for of the 10 questions I have a if -else for the correct questions and within the else the do-while to reformulate the questions. The problem is this: when I fail the question, the program reformulates it as it should do but if I enter the correct answer, the loop do-while I close the for and stop asking questions. I leave the code here.

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 I also leave the pastebin with the classes of the program and the methods. I would also appreciate suggestions to improve the code.

Program > link

Methods > link

Thank you very much

    
asked by JoshHZR 02.12.2017 в 17:23
source

1 answer

0

Since it's a school assignment and your goal is to learn, I'll give you an example of how to find random indexes , I'm not going to stick with do-while, nor while, > only with random or random numbers.

Copy and paste the code into a class that is named " Panel " and give it " Run as java application strong> ", then analyze what happens by console every time you give repeatedly to" > Run as java application ", see if it works for your random question program.

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Random;

public class Panel {

    public void arrayListRandom() {

            List<String> list = new ArrayList<>();
            list = Arrays.asList("Nombre?", "Apellido?", "Edad?", "Cancion?", "Estudios?");

            Random random = new Random();
            Integer entero = random.nextInt(list.size());

            System.out.println(entero);

    }

    public static void main(String[] args) {
        Panel p = new Panel();
       p.arrayListRandom();
    }

}

For each time you run the program the console prints a different integer, taking as the maximum size within which the maximum size of the ArrayList moves, it can also be made simpler by using Arrays or simple vectors.

The result is absolutely the same, to begin with I recommend you use them over any type of Collection.

import java.util.Random;

public class Panel {

    public void arrayListRandom() {

        String[] list = { "Nombre?", "Apellido?", "Edad?", "Cancion?", "Estudios?" };

        Random random = new Random();
        Integer entero = random.nextInt(list.length);

        System.out.println(entero);

    }

    public static void main(String[] args) {
        Panel p = new Panel();
        p.arrayListRandom();
    }

}
    
answered by 02.12.2017 в 18:12