【问题标题】:Trouble understanding loops and arrays无法理解循环和数组
【发布时间】:2018-03-27 01:24:42
【问题描述】:

如何更改以下过程,使其使用循环,循环由文件开头的数字控制?我还想添加一个固定大小的记录数组来存储读取的每条记录。我对所有这一切都比较陌生,因此感谢您提供任何帮助。

procedure ReadLinesFromFile(var myFile: TextFile);
    var p: Person;
        number: Integer;
    begin
     ReadLn(myFile, number);
     ReadLn(myFile, p.name);
     ReadLn(myFile, p.age);
     ReadLn(myFile, p.name);
     ReadLn(myFile, p.age);
     ReadLn(myFile, p.name);
     ReadLn(myFile, p.age);
     ReadLn(myFile, p.name);
     ReadLn(myFile, p.age);
     ReadLn(myFile, p.name);
     ReadLn(myFile, p.age);
    end;

【问题讨论】:

  • 为什么你用不同的帐户发布这个,而不是你同时发布的其他 q?

标签: arrays loops pascal procedure


【解决方案1】:

你必须声明一个数组,比如

people: array [1..100] of person

然后按如下方式填写

 ReadLn(myFile, number);
 for i:= 1 to number do
  begin 
   ReadLn(myFile, people[i].name);
   ReadLn(myFile, people[i].age);
  end;

最好使用动态数组,因为上面的代码假设文件中最多有 100 条记录。也没有检查文件是否实际包含“数字”记录。这些留给“读者练习”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2014-02-26
    相关资源
    最近更新 更多