java - 如何使用类 Math 用从 0 到 99 的随机数填充数组?

标签 java arrays random

我写了代码,但是没有从double到int的转换。

public class Array {
    public static void main(String[] args) {
        int i;
        int[] ar1 = new int[100];
        for(int i = 0; i <  ar1.length; i++) {
            ar1[i] = int(Math.random() * 100);
            System.out.print(ar1[i] + "  ");
        }
    }
}

如何纠正?

最佳答案

 ar1[i] = (int)(Math.random() * 100);

Java 中的转换看起来像 C 中的强制转换。

关于java - 如何使用类 Math 用从 0 到 99 的随机数填充数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25768435/

相关文章:

java - jpa 标准比较两个字符串而不考虑空格

java - 如何匹配模式的第一个实例?

java - 如何从 ActionListener 获取值

检查 C 编程代码

javascript - 使用 for 循环 javascript 比较数组元素

java - "In a distributed environment, one does not use multithreding"- 为什么?

c++ - 如何在 C++ 中将指针表示的二维数组转换回二维数组?

objective-c - 为什么我使用 NSInteger 和 arc4random 会得到奇怪的垃圾?

python - 在 C 中生成幂律分布并使用 python 进行测试

c++ - Random_shuffle 可以在没有 <algorithm> 库的情况下运行吗?