从记录中查找最高值

发布于 2024-11-09 17:04:39 字数 491 浏览 0 评论 0原文

我正在尝试编写一个过程,该过程会遍历数字记录并找到最高的数字,当前的代码如下。我遇到的问题是,它似乎只是列出了记录中的最后分数(而不是最高分数)。非常感谢任何帮助。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怂人 2024-11-16 17:04:39

您输出的不是 Highest,而是 TopScores[Count].Score。只需使用

 Writeln('Highest is ', Highest, ' for ', Name);

另外,您应该将名称放入 if 语句内的变量 Name 中(它实际上在外部)。

插件:如果您想要所有名字以防平局,您可以使用以下代码

Highest := 0;
For Count := 1 to MaxSize Do Begin
     If TopScores[Count].Score = Highest Then Begin
         Name := Name + ' and ' + TopScores[Count].Name;
     End;
     If TopScores[Count].Score > Highest Then Begin
         Highest := TopScores[Count].Score;
         Name := TopScores[Count].Name;
     End;
 End;

You're not outputting Highest, but TopScores[Count].Score. Just use

 Writeln('Highest is ', Highest, ' for ', Name);

Also 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

Highest := 0;
For Count := 1 to MaxSize Do Begin
     If TopScores[Count].Score = Highest Then Begin
         Name := Name + ' and ' + TopScores[Count].Name;
     End;
     If TopScores[Count].Score > Highest Then Begin
         Highest := TopScores[Count].Score;
         Name := TopScores[Count].Name;
     End;
 End;
红焚 2024-11-16 17:04:39

除了霍华德的答案之外,在开始循环之前将“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.

信愁 2024-11-16 17:04:39

除了接受的答案之外,请确保打开警告和提示,您会看到:

      testhighest.pp(16,39) Warning: Local variable "Highest" does not seem to be initialized

哪一

        If TopScores[Count].Score > Highest Then

In addition to the accepted answer, make sure you turn on your warnings and hints, and you'll see:

      testhighest.pp(16,39) Warning: Local variable "Highest" does not seem to be initialized

which is the

        If TopScores[Count].Score > Highest Then

line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文