Pascal - 程序在表单切换时挂起

Pascal - Program hangs upon form switching

我目前正在开发一个简单的应用程序来计算 10 秒内的点击次数,从两个不同的文件中读取姓名和分数,然后以另一种形式以各种形式显示这些姓名和分数。然而,游戏结束后,出现 'Game Over!' 消息,出现排行榜表格但没有按钮,看起来很奇怪,而且似乎使程序崩溃。

表单应如下所示: Leaderboard Form

实际情况是这样的:Leaderboard Error in Form

显示表单的代码如下:

if TimeLeft=0 then
     begin
          Form2.Timer1.Enabled:=False;{Disable timer}
          ShowMessage('Game Over!');{Message to show upon termination condtion being met}
          Leaderboard.Show;{Show Leaderboard Form}
          Form2.Hide;{Hide game}
          Reset(LeaderboardNamesFile);{Open file}
          while not EOF(LeaderboardNamesFile) do
                LineCount:=LineCount+1;{Increment to allow for EOF marking of the score}
          LeaderboardScoresArray[LineCount]:=Score;{Add score to array of scores}
     end;

以及排行榜表格中分数显示按钮中包含的代码:

var Counter : Integer;
begin
     Counter:=1;
     Memo1.Lines.add(LeaderboardNamesArray[Counter]+' - '+IntToStr(LeaderboardScoresArray[Counter]));
end;

我觉得这一切都很奇怪,因为当显示表单时实际上没有运行任何东西,所以它应该在此之前崩溃,如果有的话,并且没有出现崩溃消息。有任何想法吗?如果需要更多信息,请询问。此站点上的新内容!

while循环是无穷无尽的,循环只包含一个命令:

while not EOF(LeaderboardNamesFile) do
            LineCount:=LineCount+1;{Increment to allow for EOF marking of the score}

因此,您的程序对 LineCount 进行了计数,但实际上并未从文件中读取任何数据。所以文件永远不会变成 EOF ("end of file")。你需要做这样的事情:

Reset(LeaderboardNamesFile);{Open file}
while not EOF(LeaderboardNamesFile) do
begin
    LineCount:=LineCount+1;{Increment to allow for EOF marking of the score}
    readln(LeaderboardNamesFile, Score);
    LeaderboardScoresArray[LineCount]:=Score;{Add score to array of scores}
end;
CloseFile(LeaderboardNamesFile);