title: How do I display the correct answer to the user's answer to the product of two random integers?
body: <p>I'm wondering how to display the correct answer right after where it says <code>"Your answer is wrong. The correct answer is "</code> in the <code>for</code> loop in the method <code>main()</code>. My problem is that the variable for the answer isn't declared until the <code>oneProblem()</code> method.</p>
  
  <pre><code>package multiplicationquiz;
  
  import static java.lang.System.out;
  import java.util.Random;
  import java.util.Scanner;
  
  public class MultiplicationQuiz {
  
      final static Scanner cin = new Scanner(System.in);
      final static Random rand = new Random();
  
  public static void main(String[] args) {
      out.println("Welcome to the Multiplication Table Quiz\n");
      out.print("Enter your given name (no spaces): ");
      String name = cin.next();
      out.println("\nGreetings, " + name + ". I will ask you 10 problems.");
      out.println("Ready, set, go!");
  
      int qno;
      int numCorrect = 0, numProbs = 10;
      for (qno = 1; qno &lt;= 10; qno++){
          if (oneProblem(qno) == true){
              out.println("Your answer is correct!");
              numCorrect ++;
          }
          else
              out.println("Your answer is wrong. The correct answer is ");               
      } // end for
      report(name, numProbs, numCorrect);
  } // end main
  
  static boolean oneProblem(int qno) {
      int number1 = 1 + rand.nextInt(10), number2 = 1 + rand.nextInt(10);        
  
      out.print("\nProblem " + qno + ": What is " + number1 + " times " 
                + number2 + "? ");        
      int answer = cin.nextInt();
  
      if (answer == number1 * number2)
          return true;
      else 
          return false;
  } // end oneProblem
  
  static void report(String name, int numProbs, int numCorrect) {
      out.print("Test report for " + name + "\n\n");
      out.print("Problems attempted: " + numProbs + "\n");
      out.print("Correct answers: " + numCorrect + "\n");
      out.print("Incorrect answers: "  + (numProbs - numCorrect) + "\n");
  
      if (numCorrect == 10)
          out.print("Congratulations! You got a perfect score.");
      if (numCorrect &lt; 10 &amp;&amp; numCorrect &gt;= 8)
          out.print("You did all right.");
      if (numCorrect &lt; 8 &amp;&amp; numCorrect &gt;= 7)
          out.print("You pass, but you should try to do better.");
      if (numCorrect &lt; 7)
          out.print("You really need to study harder.");
  
  } // end report
  static int getInt(String prompt) {
      out.print(prompt);
      return cin.nextInt();
      }  
  }
  </code></pre>