从记录中查找最高值
我正在尝试编写一个过程,该过程会遍历数字记录并找到最高的数字,当前的代码如下。我遇到的问题是,它似乎只是列出了记录中的最后分数(而不是最高分数)。非常感谢任何帮助。
Procedure FindTopScore(Var TopScores : TTopScores);
Var
Count : Integer;
Highest : Integer;
Name: String;
Begin
For Count := 1 to MaxSize Do
If TopScores[Count].Score > Highest Then
Highest := TopScores[Count].Score;
Name := TopScores[Count].Name;
Writeln('Higest score is by ' ,TopScores[Count].Name, ' of ', TopScores[Count].Score);
End;
I'm trying to code a procedure which goes through a record of numbers and finds which one is highest, the code currently is below. The issue I have is that it just seems to list the last score on the record (not the highest). Any help is greatly appreciated.
Procedure FindTopScore(Var TopScores : TTopScores);
Var
Count : Integer;
Highest : Integer;
Name: String;
Begin
For Count := 1 to MaxSize Do
If TopScores[Count].Score > Highest Then
Highest := TopScores[Count].Score;
Name := TopScores[Count].Name;
Writeln('Higest score is by ' ,TopScores[Count].Name, ' of ', TopScores[Count].Score);
End;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您输出的不是
Highest
,而是TopScores[Count].Score
。只需使用另外,您应该将名称放入 if 语句内的变量
Name
中(它实际上在外部)。插件:如果您想要所有名字以防平局,您可以使用以下代码
You're not outputting
Highest
, butTopScores[Count].Score
. Just useAlso you should put the name into a variable
Name
inside the if-statement (it actually is outside).Addon: If you want all names in case of a tie you can use e.g. the following code
除了霍华德的答案之外,在开始循环之前将“0”设置为“最高”。由于未初始化,它具有任意值,可能高于最高分。
In addition to Howard's answer, set '0' to 'Highest' before beginning the loop. Being uninitialized, it is having an arbitrary value, probably higher then the highest score.
除了接受的答案之外,请确保打开警告和提示,您会看到:
哪一
行
In addition to the accepted answer, make sure you turn on your warnings and hints, and you'll see:
which is the
line