私はJavaを初めて使用し、今学期のプログラミングコースでJavaを学んでいます。宿題の期限があり、苦労しています。経験豊富なプログラマーにとってこれは簡単なことだと思いますが、私にとっては難問です。これが最初の質問です。
public int countInRange(int[] data, int lo, int hi)
このためには、配列 data の要素数をカウントする必要があります。これは、>>lo から hi までの範囲内にあり、そのカウントを返します。たとえば、データが配列 {1, 3, 2, 5, 8} の場合 >>then 呼び出し
countInRange(データ, 2, 5)
2 .. 5 の範囲には 3、2、5 の 3 つの要素があるため、3 を返す必要があります。
そして、これが私がこれまでに行ったことです:
/**
* Count the number of occurrences of values in an array, R, that is
* greater than or equal to lo and less than or equal to hi.
*
* @param data the array of integers
* @param lo the lowest value of the range
* @param hi the highest value of the range
* @return the count of numbers that lie in the range lo .. hi
*/
public int countInRange(int[] array, int lo, int hi) {
int counter = 0;
int occurrences = 0;
while(counter < array.length) {
if(array[counter] >= lo) {
occurrences++;
}
counter++;
}
return occurrences;
}