如何使用Math类填充0到99之间随机数的数组?

Yak*_*kov 2 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] + "  ");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

怎么纠正?

tal*_*lex 6

 ar1[i] = (int)(Math.random() * 100);
Run Code Online (Sandbox Code Playgroud)

Java中的转换看起来像是C中的转换.

  • 并且也被称为铸造. (8认同)

Thu*_*tne 5

它应该像

 ar1[i] = (int)(Math.random() * 100);
Run Code Online (Sandbox Code Playgroud)

转换时,转换类型应在括号中,例如 (cast type)value