私は次のアルゴリズム(擬似コード)からJavaコードのメソッドを作成できるようにする割り当てを持っている:アルゴリズムの実装から間違った結果が得られるのはなぜですか?
Design Algorithm CalcLowest
var lowest = first value in the array numList
For index=1 to count-1
If value at index is less than lowest
Set lowest to value at index
End if
End loop
Design Algorithm CalcHighest
var highest = first value in the array numList
For index=1 to count-1
If value at index is greater than highest
Set highest to value at index
End if
End loop
私は次のように書かれている:
public int getHighest()
{
int highest = marks[0];
for (int i=0; i < count +1; i++){
if(i > highest)
highest = i;}
return highest;
}
public int getLowest()
{
int lowest = marks[0];
for (int i=0; i < count +1; i++){
if(i < lowest)
lowest = i;}
return lowest;
}
}
それが実行されますが、最高値は常に5です最低は常に0です。なぜですか?
あなたは '最低にi'を比較している、あなたは最低 –
に'マーク[i]は 'を比較する必要があるおかげで、その場合は – Delamitri