我正在尝试将我的整数数组“ Arr”传递给函数“ collect”。在collect的内部,我正在从用户那里收集一个int。一旦用户输入100个整数或给出0或负数的值,数组将停止收集。我在做什么错?
with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO;
procedure helloworld is
procedure PrintHelloWorld is
begin
Put_Line ("Enter Integers, up to 100 individual ints.");
end PrintHelloWorld;
function collect (A : array) return array is
A: array (1 .. 100) of integer;
begin
Ada.Text_IO.Put ("Enter an integer: ");
Ada.Integer_Text_IO.Get(I);
Ada.Text_IO.Put_Line (Integer'Image(I));
if I > 0 then
A(i) := I;
end if;
while I > 0 loop
Ada.Text_IO.Put ("Enter an integer: ");
Ada.Integer_Text_IO.Get(I);
Ada.Text_IO.Put_Line (Integer'Image(I));
if I > 0 then
A(i) := I;
end if;
end loop;
return A;
end collect;
procedure printArr is
begin
for j in Arr'range loop
Ada.Text_IO.Put_Line(Integer'Image(Arr(j)));
end loop;
end printArr;
Arr: array (1 .. 100) of integer;
Arr := (others => 0);
I: Integer;
begin
Put_Line ("Hello World!");
PrintHelloWorld;
Arr := collect(Arr);
printArr;
end helloworld;
终端错误:
gnatmake helloworld.adb
x86_64-linux-gnu-gcc-8 -c helloworld.adb
helloworld.adb:13:26: anonymous array definition not allowed here
helloworld.adb:13:46: reserved word "is" cannot be used as identifier
helloworld.adb:15:08: missing ")"
gnatmake: "helloworld.adb" compilation error
似乎您的思维陷入了C / C ++ / Java语法和Ada之间。请尝试以下示例。
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Main is
type Data_Array is array(Positive range <>) of Integer;
function Collect return Data_Array is
Count : Natural := 0;
begin
Put_Line("Enter the number of data values you will input");
Get(Count);
declare
Nums : Data_Array(1..Count);
begin
Put_Line("Enter" & Count'Image &" values");
for Value of Nums loop
Get(Value);
end loop;
return Nums;
end;
end Collect;
procedure Print(Item : in Data_Array) is
begin
Put_Line("Output of array values:");
for Value of Item loop
Put_Line(Value'Image);
end loop;
end Print;
begin
Print(Collect);
end Main;
过程Print接受Data_Array类型的参数,该参数由Collect函数的返回值提供。