2

「lowestScore」メソッドを作成するときにエラーが発生したと思われます (「largestScore」メソッドは一貫して正しい値 (配列からの最高スコア) を返すため、多かれ少なかれ否定しました)。しかし、何らかの理由で、lowestScore メソッドは配列からでもなく、配列の最初の要素または任意の数値を返すだけです。

    public static double highestScore(double[] scores)
    {
      double max = scores[0];
      for (int i=0; i < scores.length; i++) {
        if (scores[i] > max) {
          max = scores[i];
        }
      }
      return max;
    }

    public static double lowestScore(double[] scores)  //possible problem some where in
    {                                                  //here?
      double min = scores[0];
      for (int i=0; i > scores.length; i++) {
        if (scores[i] < min) {
          min = scores[i];
        }
      }
      return min;
    }
4

2 に答える 2

6

はい、問題は にありlowestScoreます。<と の両方を逆にしまし>たが、それでも配列全体をループする必要があります。あなたのコードでi > scores.lengthは、(0 > scores.length最初​​は)が と評価されるfalseため、ループは実行されず、min常に と等しくなりscores[0]ます。

変化する

for (int i=0; i > scores.length; i++)

for (int i=0; i < scores.length; i++)
于 2013-05-07T14:23:04.087 に答える
2
public static double lowestScore(double[] scores)  //possible problem some where in
{                                                  //here?
  double min = scores[0];
  for (int i=0; i > scores.length; i++) {
    if (scores[i] < min) {
      min = scores[i];
    }
  }
  return min;
}

ラインfor (int i=0; i > scores.length; i++) {i条件は、「次の値より大きい場合はループを継続するscores.length」というものです。がi0 に初期化されると、配列のサイズより大きくなることはありません。したがって、ループは即座に終了し、戻り値はループの前に設定された配列の最初の要素になります。

PS 間違いを理解するの<>簡単highestScoreですlowestScore

于 2013-05-07T14:25:55.557 に答える