如何在delphi中跟踪点数

时间:2015-09-27 07:32:58

标签: delphi

我正在努力寻找一种能够跟踪积分的方法。我为我的高中第三学期项目编写了一个教育游戏,我的教育游戏是一系列多项选择词问题,我想要发生的是,每次用户点击正确答案时,都应添加一个点这样,当用户到达结束页面并点击“获得结果”时,按钮,delphi将计算用户获得的所有正确答案,并在弹出消息中显示它们。

1 个答案:

答案 0 :(得分:2)

虽然David Heffernan在他的评论中建议的解决方案最容易实现(在转到下一个问题之前,您只需检查用户是否正确回答并在转移到下一个问题之前增加积分)我宁愿选择推荐的解决方案paulsm4。为什么?

他的解决方案建议存储用户所做的所有答案,这些答案最终会为您提供能力,以便您可以向用户显示他正确回答哪些问题,哪些问题没有,哪些是正确答案。

现在,因为你正在制作一个教育游戏,向用户显示他搞砸了,他的答案应该会大大提高你的应用程序的价值和可用性。

那么你如何实现这个:

首先,我建议创建一个记录,用于存储有关每个问题的信息,如此

RQuestion = record
  //Contains the text which represents the question
  QuestionText: String;
  //Contains text which represents first answer
  Answer1Text: String;
  //Contains text which represents second answer
  Answer2Text: String;
  //Contains text which represents third answer
  Answer3Text: String;
  //Contains text which represents fourth answer
  Answer4Text: String;
  //Simple integer value to store which is the correct answer
  CorrectAnswer: Integer;
  //Simple integer value to store which answer was chosen by the user
  SelectedAnswer: Integer;
end;

然后,为了存储所有问题所需的数据,你可以简单地制作一组RQuestion类型的项目

ArrQuestions: Array[0..NumOfQuestions] of RQuestion;

现在,您可以访问有关每个问题的数据,如下所示:

var Questions: ArrQuestions;

...

//Text of the question
QText := Questions[QNum].QuestionText;
//Text of the first answer
A1Text := Questions[Qnum].Answer1Text;
//Text of the second answer
A2Text := Questions[Qnum].Answer2Text;
//Text of the third answer
A3Text := Questions[Qnum].Answer3Text;
//Text of the fourth answer
A4Text := Questions[Qnum].Answer4Text;
//Correct answer
CorrAnsw := Questions[Qnum].CorrectAnswer;
//Chosen answer
SelAnsw := Questions[QNum].SelectedAnswer;

然后,为了计算用户累积的积分数,您只需循环查看问题数组并检查用户是否正确回答了问题

TotalPoints := 0;
for Qnum := 0 to NumOfQuestions do
begin
  if Questions[QNum].CorrectAnswer = Questions[QNum].SelectedAnswer then
    TotalPoints := TotalPoints+1;
end;

正如我之前所说,最后你可以让你的用户有能力回顾他的答案,看看他选择了哪些,哪些是正确的。

这样可以提高用户的学习能力。

奖金内容

当您在程序中使用记录和记录数组时,您可以使用键入的文件从文件中快速加载这些记录中的信息。

//Assign file handle
AssignFile(QFile,'D:\Questions.dat');
//Open file in read only mode
Reset(QFile);
//Set the desired question umber that you want to read
QNume := 10;
//Move file position to the specific question
//Exact position in bytes is automatically calculated
//since we are using typed files
Seek(QFile,QNum);
//Read the data from file into specific variable which can
//Also be a record inside an array
Read(QFile,Questions[QNum]);
//Close the file handle when you are done
CloseFile(QFile);

但是,您应该注意,为了做到这一点,您的记录类型需要具有固定的大小。这意味着记录中的每个字段/变量都需要具有固定大小。

不幸的是,在字符串的情况下,情况并非如此,因为Delphi字符串实际上是引用类型,它们的大小实际上取决于存储在这种字符串中的文本。

因此,如果您希望能够直接从类型文件加载此类记录,则必须将所有字符串更改为短字符串

RQuestion = record
  //Contains the text which represents the question
  QuestionText: ShortString;
  //Contains text which represents first answer
  Answer1Text: ShortString;
  //Contains text which represents second answer
  Answer2Text: ShortString;
  //Contains text which represents third answer
  Answer3Text: ShortString;
  //Contains text which represents fourth answer
  Answer4Text: ShortString;
  //Simple integer value to store which is the correct answer
  CorrectAnswer: Integer;
  //Simple integer value to store which answer was chosen by the user
  SelectedAnswer: Integer;
end;

现在唯一的问题是ShortStrings只支持基于ANSI的字符。因此,您可能无法在文本中使用某些非美国字符。

如果您需要UNICODE字符串,则必须使用不同的方法。在这种方法中,您不会将文本存储在记录中,而是分别存储在StringList中。并且您的记录将只存储有关特定StringList条目的索引信息,该条目包含特定于您的问题/答案的文本

所以问题记录现在看起来像这样

RQuestion = record
  //Contains StringList index at which the question text is stored
  QuestionText: Integer;
  //Contains StringList index at which the first answer text is stored
  Answer1Text: Integer;
  //Contains StringList index at which the second answer text is stored
  Answer2Text: Integer;
  //Contains StringList index at which the third answer text is stored
  Answer3Text: Integer;
  //Contains StringList index at which the fourth answer text is stored
  Answer4Text: Integer;
  //Simple integer value to store which is the correct answer
  CorrectAnswer: Integer;
  //Simple integer value to store which answer was chosen by the user
  SelectedAnswer: Integer;
end;

所以现在你有了固定大小的记录,可以很容易地从类型文件中加载或保存。您可以使用String Lists LoadFromfile或SaveToFile方法轻松地将问题和答案文本加载或保存到StringList中或从StringList中解决。

关于这最后一种方法的最好的事情是你甚至可以拥有多个文本文件,其中每个文本文件都包含用不同语言编写的文本,这使您可以轻松地使用您的应用程序设计多项测试。