简体   繁体   中英

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 !

Let me know if this helps. I just added a for loop to ask the user for input 10 times and then built the +/- based off whether a random int was even or odd.

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

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

    // Random number generating
    // Ask the user for input 10 times
    for (int i = 0; i < 10; i++) {
      Random generator = new Random();

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

      // if N3 is even use plus, if odd use minus
      Boolean plus = N3 % 2 == 0;

      // Set a string to either "+" or "-" depending on the value of plus
      String plusMinus = plus? "+" : "-";

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

      // Display if its correct or not
      // Set the answer depending on the value of plus.
      check = plus? N1 + N2 : N1 - N2;

      if(userAnswer == check){
        System.out.println("You are correct!");
      }
      else{
        System.out.println(" Sorry, the correct answer is : " + check);
      }
    }

  }
}

So I came up with a quick solution that would look something like this

Random generator = new Random();
int var1 = generator.nextInt(100);
int var2 = generator.nextInt(100);
int choice = generator.nextInt(3);

switch (choice){
    case 0:
         System.out.println(var1 + " + " + var2);
         break;
    case 1:
         System.out.println(var1 + " - " + var2);
         break;
    case 2:
         System.out.println(var1 + " * " + var2);
         break;
    case 3:
         System.out.println(var1 + " / " + var2);
         break;
 }

This is just a rough solution, nothing fancy, but hopefully gives you the idea of what you're looking for.

You needed a choice of one of 4 operators, so I've randomly picked a number from 0-3 to get a random operator, which we can then use in a switch to generate a question based on our 2 random numbers

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