如何使用Math类使用从0到99的随机数填充数组? [英] How to fill an array with random numbers from 0 to 99 using the class Math?

查看:56
本文介绍了如何使用Math类使用从0到99的随机数填充数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote the code, but there is no conversion from double to 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);

投射时,投射类型应放在方括号内,例如(广播类型)值

When you cast, cast type should be in brackets e.g. (cast type)value

这篇关于如何使用Math类使用从0到99的随机数填充数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆