Java数字随机数组 [英] Java random array of numbers

查看:78
本文介绍了Java数字随机数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个范围为0-9的随机数数组,其中一个变量是kickerNumbers。我说不兼容的类型,但出现编译错误。我曾尝试将(int)更改为 [int] ,但我不认为那会是无论如何,而且事实并非如此。还有另一种写方法吗?我需要它充当前4个数字,以0-9表示,但第5个数字必须较小,例如0-4。对于0-4,我只是做了一个完全不同的变量。那是一个好选择吗?这是给出错误的行的示例代码。

I am trying to make an array of random numbers that range from 0-9 with the variable for this one being kickerNumbers. I get a compile error though saying incompatible types. I have tried changing the (int) to [int] like it said but I didn't think that would be right anyway and surly enough it wasn't. Is there another way to write this? I need it to act as the first four numbers of this to be 0-9 but the fifth number would need to be something smaller such as 0-4. for the 0-4 one I just made that a completely different variable. Was that a good choice? Here is the sample code for the lines that give the error.

import java.util.Scanner;
import java.util.InputMismatchException;
public class CashBallTest
{
    public static void main(String[]args)
    {
        Scanner keyboard = new Scanner(System.in);
        int kicker;
        int[] kickerNumbers = (int)(Math.random()*0+9);
        int kickerPowerball=(int)(Math.random()*0+4);

错误为:

\CashBallTest.java:9: incompatible types
    found   : int
    required: int[]
    int[] kickerNumbers = (int)(Math.random()*0+9);

我也尝试过更改(int)(int []),但随后它表示它是不可转换的类型,当它需要是 int [] ,但我看不到它从何处获取双精度类型。

I have also tried changing (int) to (int[]) but then it said it was an inconvertible type counting as a double when it needs to be an int[] but I don't see where it gets the double type from.

推荐答案

int[] kickerNumbers = (int)(Math.random()*0+9);

您不能将 int 分配给 int []

假设应该更多的是一个数字,可能是这样的

Assuming that is supposed to be more the one number, it might be something like this

int[] kickerNumbers = new int[5];
for(int i = 0; i < kickerNumbers.length; i++) {
    kickerNumbers[i] = (int)(Math.random()*0+9);
}

在不相关的音符上,为什么要乘以 Math.random()* 0 ?这对我来说似乎很愚蠢。如果您想要一个1到10的数字,我可以这样做: Math.random()* 10 + 1

On an unrelated note, why are you multiplying the Math.random()*0? That seems silly to me. If you want a number 1 through 10, I would do this: Math.random()*10 + 1

这篇关于Java数字随机数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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