我正在尝试编写一个程序,该程序将返回数组中包含等于插槽索引的值的插槽数,生成随机数后,然后在单独的行上打印数组的每个元素阿斯特里克斯 (假设看起来像这样:)
Enter the array size: 4
a[0] = 2
a[1] = 1 *
a[2] = 0
a[3] = 2
There are 1 slots where the index is the same as the value.
但我失败得很惨,任何朝着正确方向轻推都会受到赞赏。这是我到目前为止所拥有的:
import java.util.Scanner;
public class RandomArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i;
System.out.println("Enter the array size: ");
i = in.nextInt();
}
/**
* Fills an array with random integers in the range from zero to one less
* than the array size.
*
* @param array
* the array to fill with random integers
*/
public static void fillArrayWithRandomInts(int array[]) {
java.util.Random random = new java.util.Random(13);
for (int i = 0; i < array.length; ++i)
array[i] = (int) (random.nextDouble() * array.length);
}
public static int countSlotsWithIndexEqualToValue(int array[]) {
int[] value = new int[13];
for (int i = 0; i < array.length; i++) {
if (value[13] == array[13])
;
}
return value[13];
}
public static void printArray(int array[]) {
}
}