返回java中给定值的任意数字

时间:2019-05-11 12:36:50

标签: java random

因此,Im试图制作一个函数,该函数返回大于X且不大于1,000,000,000且以0结尾的任意整数。您可以假定X在1到999,999,999之间。例如,给定X = 33,则函数可能返回77,对于X = 22,您的函数可能返回92。

这是我到目前为止所得到的,不确定即时消息是否做对了...

import java.util*;
import java.io*;

public class exerciseA {
    public static void main(String[] args) throws Exception {
        int max = 1000000000;
        int min = 0;
        int diff = max - min;
        Random arbitrary = new Random();
        int i = arbitrary.nextInt(diff + 1);
        i += min;
        System.out.print("The arbitrary Number is " + i);
    }
}

1 个答案:

答案 0 :(得分:1)

以下代码片段将达到目的:

int max = 100000000;                                     // change made here
int min = 0;
int diff = max - min;
Random arbitrary = new Random();
int i = arbitrary.nextInt(diff + 1);
i += min;
System.out.print("The arbitrary Number is " + i * 10);  // change made here

注意:

将max初始化为100000000,因为我们将任意数乘以10。