-3

In this block of code, at line thirteen, I incremented correctAnswers by 1. However, once the if statement has broke, The value is just one (or zero sometimes) when it prints the percentage. Can anyone tell me what is wrong with my code?

private static void multiplicationTest(int maxNumber, int minNumber) {  
    int i = 1;                                                          

    while(i != 11) {                                                    
        int firstNumber = (minNumber + (int)(Math.random() * ((maxNumber - minNumber) + 1)) ), secondNumber = (minNumber + (int)(Math.random() * ((maxNumber - minNumber) + 1)) );
        int inputAnswer, answer = (firstNumber * secondNumber);
        int correctAnswers = 0;

        System.out.print("Question " + i + ".)\t" + firstNumber + " * " + secondNumber + " = ");
        inputAnswer = input.nextInt();

        if(inputAnswer == answer) {
            correctAnswers++;
            System.out.print("\tcorrect\n");

        } else {
            System.out.print("\tincorrect --- " + firstNumber + " * " + secondNumber + " = " + answer + "\n");

        } if(i == 10) {
            System.out.println("\nYou scored " + correctAnswers + " out of 10 - " + (correctAnswers * 10) + "%.");

        }

        i++;
    }

}
  • 1
    *"However, once the if statement has broke..."* What does that mean? – T.J. Crowder Sep 11 '16 at 15:43
  • 6
    Also, you have some *very* misleading bracing going on at the end: `} if(i == 10) {` Start that `if` on a line of its own. – T.J. Crowder Sep 11 '16 at 15:44
  • 2
    You're iterating 10 times. On each iteration you set `correctAnswers = 0`. How do you expect it to remember total count across all 10 iterations, if you keep resetting it to 0? – Andreas Sep 11 '16 at 15:46
  • Please see [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149) – Andreas Sep 11 '16 at 15:48

2 Answers2

2

Place int correctAnswers = 0; before the while line.

If its in the while loop it will keep resetting the overall score on every run.

This means the score is really out of one each time, not 10.

UserF40
  • 3,533
  • 2
  • 23
  • 34
0

UserF40 has pointed out your error. For the rest, I will do a short code review.

  1. You are using a while loop to count from 1 to 10. It is much simpler to use a for loop to do that: for (int i = 1; i < 11; ++i). Use a while loop for when there is a non-numerical boolean condition to terminate the loop.

  2. Your second if is misplaced, and can be omitted. Since it only takes action on the last pass through the loop, you can put the action after the loop terminates, without any need for the if.

  3. Learn the Java libraries. You Math.random() is good for real numbers, but not good for integers. For random integers use Random.nextInt() which supplies integers directly.

  4. Avoid multiple declarations on one line, it is unnecessary and can be confusing. One declaration per line is clearer.

rossum
  • 15,344
  • 1
  • 24
  • 38
  • Thanks. On number three, I use `Math.random()` because I learned that line to make random integers within a range. If there is a line similar using `Random.nextInt()`, is it the same or different? – Nynthes K'looguee Sep 11 '16 at 19:09
  • `Random.nextInt(int boundary)` will give you a random integer from 0 (inclusive) to boundary (exclusive). For your code it will look something like: `firstNumber = minNumber + Random.nextInt(maxNumber - minNumber +1);` My "+ 1" assumes that maxNumber is included in the range of numbers produced. This is documented in the [Java documentation](http://docs.oracle.com/javase/8/docs/api/index.html). – rossum Sep 11 '16 at 20:12