如何重新运行静态整数数组?

huangapple go评论87阅读模式
英文:

How do I rerun a static integer array?

问题

这是我想要重新运行的数组:

public static int[] rollDice(int dice[]) {
    // 生成 5 个随机数 / 更新 dice 数组
    for (int i = 0; i < dice.length; i++) {
        dice[i] = (int)(Math.random() * 6 + 1);
    }

    return dice;
}

如果我想重置这个数组并找到新的随机数,我该如何做呢?我尝试了 rollDice(),但只得到了一个错误。

英文:

here's the array I want to rerun:

public static int[] rollDice(int dice[]) {
    // generate 5 random numbers / update dice array
    for (int i = 0; i &lt; dice.length; i++) {
        dice[i] = (int)(Math.random() * 6 + 1);
    }

    return dice;
}

If I want to reset this array and find new random numbers how would I do that? I tried rollDice() only to get an error.

答案1

得分: 1

没有必要返回数组,因为在调用方法rollDice()时,您已经拥有数组的引用。

数组通过引用传递,而不是通过值传递,这意味着您并没有像处理int一样使用副本,而是在修改原始数组。

将返回类型更改为void,然后移除返回语句,您的代码应该可以按预期工作。

英文:

There is no point in returning the array, since you already have a reference to the array when you call the method rollDice().

Arrays are sent by reference and not by value, which means you are not working with a copy like you do with ints, instead you are modifing the original array.

Change the return type to void and remove the return and your code should work as intended.

答案2

得分: 1

每次您可以获得一个包含随机数的新动态长度数组,您可以通过调用 rollDice(整数值) 来访问。

public static int[] rollDice(int length) {
     final int dice[] = new int[length];
    // 生成带有随机值的数组
    for (int i = 0; i < length; i++) {
        dice[i] = (int)(Math.random() * length + 1);
    }

    return dice;
}
英文:
You can get every time a new dynamic length array with random numbers, and you can access by call rollDice(integer value). 

public static int[] rollDice(int length) {
		 final int dice[] = new int [length];
	    // generate array with random values
	    for (int i = 0; i &lt; length; i++) {
	        dice[i] = (int)(Math.random() * length + 1);
	    }

	    return dice;
	}

答案3

得分: 0

你需要有一个类成员如下:

public static final int[] dice = new int[5];

然后使用你的方法来掷骰子/重新掷骰子,否则只需访问 dice

public static void rollDice() {
    // 生成 5 个随机数 / 更新 dice 数组
    for (int i = 0; i < dice.length; i++) {
        dice[i] = (int)(Math.random() * 6 + 1);
    }
}

有趣的事实:Java 没有像 C 和 C++ 那样的静态函数变量。在那些语言中,它可能看起来是这样的:
(我为你 Java 的朋友们写了一个类似 Java 函数的形式)

public static int[5] rollDice(boolean reroll) {
    static final int[] dice = new int[5];
    if (reroll) for (int i = 0; i < dice.length; i++) {
        dice[i] = (int)(Math.random() * 6 + 1);
    }
    return dice;
}

正如你所看到的,静态变量可以嵌入到这些函数中。如果问我,这是一个巨大的缺点,因为我经常使用它来将它们从类命名空间中隐藏起来。

英文:

You would have to have a class member like this:

public static final int[] dice = new int[5];

Then to roll/reroll the dice use your method, else just access dice.

public static void rollDice() {
    // generate 5 random numbers / update dice array
    for (int i = 0; i &lt; dice.length; i++) {
        dice[i] = (int)(Math.random() * 6 + 1);
    }
}

Interesting fact: Java has no static function variables as C and C++ does. In those languages it could look like this:
(I wrote it like a java Function for you Java guys)

public static int[5] rollDice(boolean reroll) {
    static final int[] dice = new int[5];
    if (reroll) for (int i = 0; i &lt; dice.length; i++) {
        dice[i] = (int)(Math.random() * 6 + 1);
    }
    return dice;
}

As you can see, static variables can be embedded into those functions. If you ask me, it's a huge minus, Java doesn't support this as I use it all the time to hide those from the class namespace.

huangapple
  • 本文由 发表于 2020年5月29日 21:58:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/62087668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定