配列を作成し、配列を出力し、配列で10で割り切れるすべての数を数える3つのメソッドを作成しました。私に最も迷惑を与えている部分が10.割り切れる数字をカウントしている私がこれまで持っているコードです:配列内の10で割り切れる数え算の数
public int[] createArray(int size) {
Random rnd = new Random();
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = rnd.nextInt(101);
}
return array;
}
public void printArray() {
Journal5a call = new Journal5a();
int[] myArray = call.createArray(10);
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
System.out.println("There are " + call.divideByTen(myArray[i]) + " numbers that are divisable by 10");
}
public int divideByTen(int num) {
int count = 0;
if (num % 10 == 0) {
count++;
}
return count;
}
public static void main(String[] args) {
Journal5a call = new Journal5a();
Random rnd = new Random();
call.printArray();
}
アレイ全体を渡す。その後、それをループし、if条件を呼び出して最終的なカウントを返します。 –
単一要素ではない完全配列を渡す – Hemal
'System.out.println(" + call.divideByTen(myArray [i])+ "10で割り切れる数"); "i"は範囲外です。 –