配列内の最大値の位置(インデックス)番号を表示しようとしています。 100個の乱数を持つ配列です。何らかの理由で、最初の数文字の後に出力が正しくなります。私は出力の始めに常に間違った数字を得る。これは私が持っているコードです。 おかげ配列要素のJavaでの位置表示
public static void main(String[] args) {
int max = 0;
int array[] = new int[100];
System.out.println();
// code to display student responses
randomNum(array);
System.out.println(" Survey Responses - 100 Students: ");
response(array);
System.out.println("Positions of the highest numbers");
System.out.println();
for (int i = 0; i < array.length; i++) {
max = Math.max(max, array[i]);
if (array[i] == max) {
System.out.println(i);
}
}
}
private static void randomNum(int A[]) {
for (int i = 0; i < A.length; i++) {
A[i] = (int) (Math.random() * 10000) % 10 + 1;
}
}
public static void response(int[] resp) {
for (int x = 0; x < resp.length; x++) {
System.out.print(resp[x] + " ");
}
System.out.println();
}
"配列内の最大の数字の位置"という意味を明確にしてください。固定数のインデックスを印刷しますか?現在のコードでは、配列に左から右のパスを渡すので、「これまでに見た最大の番号」のインデックスが表示されます。 –