乱数が生成された後、スロットのインデックスと等しい値を含む配列内のスロットの数を返し、配列の各要素を別の行に出力するプログラムを作成しようとしています。アスタリスク。(このように見えるとします:)
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[]) {
}
}