简体   繁体   English

为常量创建最大值可以是随机数吗?

[英]Create constant for the maximum value the random number can be?

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

Read the random class documentation at Javadoc 阅读Javadoc上的随机类文档

int upperLimit = 100;

int random1 = generator.nextInt(uperLimit);

int random2 = generator.nextInt(uperLimit);

Will give you number less than 100 会给你少于100的数字

When you call Random.nextInt(), the maximum the number can be is (2^31)-1 (2147483647). 当您调用Random.nextInt()时,最大数量可以为(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. 如果要将值传递给Random.nextInt(),例如5,则可能的结果范围是0-5。

source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM