jeudi 18 janvier 2018

Printing 10 random questions in JAVA

I am tasked with creating a quizz program in Java which asks the user to find the answer to 10 addition / subtraction problems form ( a + b ) or ( a - b) = c, where "a" and "b" are random numbers. I was able to do the first part where "a" and "b" get randomly generated and the user is told whether or not their answer is correct. I am stuck on how to generate this question 10 times and how to randomly chose whether the operator is "+" or "-". Below is the code I have so far:

import java.util.Scanner;
import java.util.Random;

public class LetsSee{
  public static void main(String [] args){
    Scanner keyboard = new Scanner(System.in);
    int sum = 0;   
    int userAnswer ;
    int check ;

    // Random number generating

    Random generator = new Random();

      int N1 = generator.nextInt(100);
      int N2 = generator.nextInt(N1);      

      System.out.println(" What is the answer to " + N1 + " + " + N2 + " = " );
      userAnswer = new Scanner(System.in).nextInt();  

      // Display if its correct or not
      check = N1 + N2;
      if(userAnswer == check){
        System.out.println("You are correct!");
      }
      else{
      System.out.println(" Sorry, the correct answer is : " + check);
      }


    }
  }

Help would be GREATLY appreciated. Thanks !




Aucun commentaire:

Enregistrer un commentaire