我有从下列算法(伪代码)制作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.为什么?
答案 0 :(得分:0)
您正在将i与最低
进行比较public ImagePanel(String imgpath) throws IOException{
try {
image = ImageIO.read(new File(imgpath));
catch (IOException e) {
// handle exception
throw new IOException();
}
}
每次循环时我都会递增。因此,最低和最高将始终与您循环的次数相同。
而不是与i比较,与数组中位置编号i 的值进行比较。