使用循环在数组中存储随机数

时间:2022-08-27 00:09:57

I need to store a random number into an array and then be able to compare the array sums at a later time. So far I have this:

我需要将一个随机数存储到一个数组中,然后能够在以后比较数组总和。到目前为止我有这个:

public class Die {
    private int dieValue = 0;
    private Random randomNumbers = new Random();
    public Die() {
}

public void rollDie() {
    dieValue = randomNumbers.nextInt(6*1);
}
public void displayDie() {
    int[] die = new int[3];
    for(int counter = 0; counter<die.length; counter++ ) {
        rollDie();
        die[counter] = dieValue;
    }
    System.out.print("Your Dies are: " + die[1] + " and " + die[2] + " and " 
    + die[3]);
    dieValue = die[1] + die[2] + die[3];
}

This gives me an error saying the array index is out of bounds, and i'm not sure how to properly code this.. any tips would be great!

这给了我一个错误,说数组索引超出范围,我不知道如何正确编码这个..任何提示都会很棒!

4 个解决方案

#1


1  

In Java, arrays start at index 0, so an array of size 3 (like you've made) will have indices 0,1,2. The out of bounds is because you're trying to used indices 1,2,3 (3 does not exist).

在Java中,数组从索引0开始,因此大小为3的数组(就像你所做的那样)将具有索引0,1,2。越界是因为你试图使用索引1,2,3(3不存在)。

Also, you're saying that you need to access the values at a later time, so you should probably declare the array outwith the method, so that it persists after the method finishes (just now, due to the scope of the variable, it will disappear after displayDie() finishes.)

此外,您说您需要稍后访问这些值,因此您可能应该使用该方法声明数组,以便在方法完成后它仍然存在(刚才,由于变量的范围,它将在displayDie()完成后消失。)

#2


1  

When you are trying to access the first value stored in an array the index should be 0, in you code above you try to access the 3 values as die[1], die[2], and die[3], but you need to be accessing them as die[0], die[1], and die[2].

当您尝试访问存储在数组中的第一个值时,索引应为0,在上面的代码中,您尝试以die [1],die [2]和die [3]的形式访问3个值,但是您需要将它们作为die [0],die [1]和die [2]访问它们。

#3


1  

Indexes for the die array in the displayDie() method are off, array index in Java starts from 0. First value will be die[0] instead of die[1].

displayDie()方法中die数组的索引关闭,Java中的数组索引从0开始。第一个值将是die [0]而不是die [1]。

#4


0  

Last line you have

你有最后一行

dieValue = die[1] + die[2] + die[3];

and suppose to be

并假设是

dieValue = die[0] + die[1] + die[2];

because there's not die[3] that's why you get the error

因为没有死[3]这就是你得到错误的原因

What you can do and will be better to loop the array

你可以做什么,并将更好地循环阵列

for(int i=0; i< die.length; i++){
   dieValue +=die[i];
}

#1


1  

In Java, arrays start at index 0, so an array of size 3 (like you've made) will have indices 0,1,2. The out of bounds is because you're trying to used indices 1,2,3 (3 does not exist).

在Java中,数组从索引0开始,因此大小为3的数组(就像你所做的那样)将具有索引0,1,2。越界是因为你试图使用索引1,2,3(3不存在)。

Also, you're saying that you need to access the values at a later time, so you should probably declare the array outwith the method, so that it persists after the method finishes (just now, due to the scope of the variable, it will disappear after displayDie() finishes.)

此外,您说您需要稍后访问这些值,因此您可能应该使用该方法声明数组,以便在方法完成后它仍然存在(刚才,由于变量的范围,它将在displayDie()完成后消失。)

#2


1  

When you are trying to access the first value stored in an array the index should be 0, in you code above you try to access the 3 values as die[1], die[2], and die[3], but you need to be accessing them as die[0], die[1], and die[2].

当您尝试访问存储在数组中的第一个值时,索引应为0,在上面的代码中,您尝试以die [1],die [2]和die [3]的形式访问3个值,但是您需要将它们作为die [0],die [1]和die [2]访问它们。

#3


1  

Indexes for the die array in the displayDie() method are off, array index in Java starts from 0. First value will be die[0] instead of die[1].

displayDie()方法中die数组的索引关闭,Java中的数组索引从0开始。第一个值将是die [0]而不是die [1]。

#4


0  

Last line you have

你有最后一行

dieValue = die[1] + die[2] + die[3];

and suppose to be

并假设是

dieValue = die[0] + die[1] + die[2];

because there's not die[3] that's why you get the error

因为没有死[3]这就是你得到错误的原因

What you can do and will be better to loop the array

你可以做什么,并将更好地循环阵列

for(int i=0; i< die.length; i++){
   dieValue +=die[i];
}