2

I want to create a simple Math game that uses two random numbers and then allows the user to input the answer for each of the following operations: (addition, subtraction, multiplication, and power). I don't know how to set the constant for a maximum a random number can be as a constant.

import java.util.Random;
import java.util.Scanner;
public class Program1 {
    public static void main(String[] args) {

         Random generator = new Random();

         // create a variable that will hold the first random number
         int random1 = generator.nextInt();

         // create a variable that will hold the second random number
         int random2 = generator.nextInt();

         // create a constant for the maximum the random number can be

2 Answers2

2

Read the random class documentation at Javadoc

int upperLimit = 100;

int random1 = generator.nextInt(uperLimit);

int random2 = generator.nextInt(uperLimit);

Will give you number less than 100

Bhanu Kaushik
  • 876
  • 6
  • 25
  • so this needs to be changed, int random1 = generator.nextInt(UpperLimit); and I need to add int UpperLimit=100;. Its telling me that UpperLimit cannot be resolved to a variable. – user2872197 Oct 11 '13 at 19:28
  • I have edited my code. This will now give 2 random numbers random1 and random2 both with upper limit 100. and if this works dont forget to accept the answer – Bhanu Kaushik Oct 11 '13 at 19:29
  • variable names in java are case sensitive. Are you sure you are using the right variable names in your code its upperLimit not UpperLimit. – Bhanu Kaushik Oct 11 '13 at 19:33
0

When you call Random.nextInt(), the maximum the number can be is (2^31)-1 (2147483647).

If you were to pass a value into Random.nextInt(), such as 5, the range of possible outcomes is 0-5.

source

LeMazing
  • 90
  • 13