0

我刚刚在学校学习 Pascal,在我的作业中遇到了一个奇怪的问题。

我需要做的是创建两个数组,然后为第一个数组读取整数,直到读取 10 个数字或读取一个负数,然后使用相同的规则移动到第二个数组。

除了第二个数组中的第一个数字总是搞砸之外,我所有的工作都很好。-1 似乎总是被复制到数组 2 索引 1。

我不能放弃太多代码,因为这是一个任务,但它是这样的:

while input >= 0 and index < 10 do
    begin
    read(input);
    array1[index] := input;
        index++
    end;

input:= 0; //to reset it

another while loop but for list2...

如果我输入 array1 1, 2, 3, -1 和 array2 1, 2, 3, 4, -1 我的输出将是这样的:

list 1: 1 list 2: -1
list 1: 2 list 2: 2
list 1: 3 list 2: 3
list 1: -1 list 2: 4

这有意义吗?我只需要一点帮助来理解为什么会这样,我被困在这里。

4

1 回答 1

1

正如对您问题的评论所指出的那样,当几乎可以肯定问题出在您未发布的代码上时,很难找到问题所在。话虽这么说,但是有一些明显的问题

  1. 在“while”循环之前读取“input” 。进入'while'循环依赖于'input'的初始值;我想你假设它的初始值为 0,但它可能是一些带有负值的垃圾数字。
  2. 'index++' 不是 Pascal 语法,而是 C。这应该是 'inc (index)'。
  3. 而不是在第一个循环之后写'input:= 0',这应该是'index:= 0'。

我想在第一个“while”循环之后的代码应该是

index:= 0;
readln (input);
while (input >= 0) and (index < 10) do
 begin
  inc (index);
  array2[index]:= input;
  readln (input) // there is no need for a semicolon before 'end'!
 end;
于 2012-09-29T05:09:23.760 回答