简体   繁体   中英

After asking the user for how many questions they want, how can I get my code to show that number of random problems?

I need to first ask the user to input how many problems they want to do. Then generate the first, then the second after they answer the first and so on.

public static void main(String[] args) {
    int number1 = (int) (Math.random() * 40 + 10), number2 = (int) (Math.random() * 40 + 10), uanswer, ianswer, counter, icounter,
            acounter, counter1, ui, aacounter, bcounter;

    Scanner input = new Scanner(System.in);

    System.out.println("How many problems do you want to do?");
    ui = input.nextInt();

    counter = 1;
    icounter = 1;

    acounter = counter + icounter;
    {
        System.out.print("What is " + number1 + " + " + number2 + "? ");
    }
    uanswer = input.nextInt();

    ianswer = number1 + number2;

    while (counter < 10000 && icounter < 1000 && acounter < 1000 && number1
            + number2 != uanswer) {

        System.out.println("Incorrect, the answer is "
                + ianswer + ", " + icounter + " out of " + icounter + " incorrect. Try again?");
        icounter++;
        acounter++;
        uanswer = input.nextInt();

    }
    if (ianswer == ianswer) {
        aacounter = acounter - 1;
        bcounter = icounter - 1;
        System.out.println("Correct, the answer is " + ianswer
                + ", " + counter + " out of " + aacounter + " correct, "
                + bcounter + " out of " + aacounter + " incorrect.");

    }

}

With my current code, I only see one problem, even though I asked for 2 or more problems at the beginning.

You need to add a loop around this statement:

System.out.print("What is " + number1 + " + " + number2 + "? ");

like:

for(int i=0; i<ui; i++){
    System.out.print("What is " + number1 + " + " + number2 + "? ");
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM