不使用记录的条件语句
Conditional Statement Not Working With Records
如何显示在某一天购买了商品的所有客户,我的代码似乎不起作用,我尝试在 displayByDayPurchased 过程中实现代码。对不起,如果这是一个简单的问题,我还是编程新手。
type
Tday = (monday, tuesday);
Tcustomer = record
itemPurchased:string;
dayPurchased: Tday;
end;
Tcustomers = array of Tcustomer;
function readDay(prompt:string): Tday;
var
selection:Integer;
begin
writeln('1. Monday');
writeln('2. Tuesday');
selection := ReadIntegerRange('Select day purcased (1 - 3): ', 1, 3);
result := Tday(selection-1);
end;
function readCustomers(prompt:string):TCustomers;
var
numOfCustomers:integer;
i:integer;
begin
numOfCustomers := ReadInteger('Enter number of customers: ');
setLength(result, numOfCustomers);
for i := 0 to high(result) do
begin
writeln(i);
result[i].itemPurchased := ReadString('Item Purchased: ');
result[i].dayPurchased := readDay(prompt);
end;
end;
procedure displayByDayPurchased(customers:TCustomers);
var
specific_day:integer;
begin
specific_day := ReadInteger('Enter day to see items purchased');
if (specific_day = customers.dayPurchased[specific_day])then
begin
end;
end;
procedure Main();
var
customer: Tcustomers;
begin
customer := readCustomers('Read in customers');
end;
begin
Main();
end.
my code doesn't seem to work, ive tried implementing the code within the displayByDayPurchased procedure.
好吧,在您发布的代码中,您的 displayByDayPurchased
实际上并没有实现任何会导致显示匹配记录的内容,您所拥有的只是一个空的 begin ... end
块:
if (specific_day = customers.dayPurchased[specific_day])then
begin
end;
和 a) 无论如何都没有正确指定匹配条件和 b) 它不会编译,因为 Tcustomers
数组没有 dayPurchased
字段( Tcustomer
记录有,但数组没有)。
你的代码依赖了很多你没有提供定义的东西(ReadString
,ReadInteger
,ReadIntegerRange
,等等)所以很难给你一个测试解决方案。
然而,您的 displayByDayPurchased
的实现可能看起来像这样:
procedure displayByDayPurchased(customers:TCustomers);
var
specific_day:integer;
i : integer;
ADay : Tday;
begin
specific_day := ReadInteger('Enter day to see items purchased');
ADay := Tday(specific_day); // converts integer to TDay value
for i := Low(customers) to High(Customers) do begin
if customers[i].dayPurchased = ADay then begin
writenln(customers[i].itemPurchased);
end;
end;
end;
我假设您的 Tcustomer
记录实际上包含客户的姓名,并且您的代码需要修改才能处理。
顺便说一句,你的 function readDay(prompt:string): Tday
是错误的;由于您对 Tday
的定义,readDay
中允许的值应该是 0 和 1,因为 Tday
枚举的最低值实际上对应于零,而不是 1。
此外,您没有说明您使用的是哪个 Pascal 编译器,但大多数现代版本都允许像 Tday(integerValue)
这样的调用将整数转换为枚举的实例值。
如何显示在某一天购买了商品的所有客户,我的代码似乎不起作用,我尝试在 displayByDayPurchased 过程中实现代码。对不起,如果这是一个简单的问题,我还是编程新手。
type
Tday = (monday, tuesday);
Tcustomer = record
itemPurchased:string;
dayPurchased: Tday;
end;
Tcustomers = array of Tcustomer;
function readDay(prompt:string): Tday;
var
selection:Integer;
begin
writeln('1. Monday');
writeln('2. Tuesday');
selection := ReadIntegerRange('Select day purcased (1 - 3): ', 1, 3);
result := Tday(selection-1);
end;
function readCustomers(prompt:string):TCustomers;
var
numOfCustomers:integer;
i:integer;
begin
numOfCustomers := ReadInteger('Enter number of customers: ');
setLength(result, numOfCustomers);
for i := 0 to high(result) do
begin
writeln(i);
result[i].itemPurchased := ReadString('Item Purchased: ');
result[i].dayPurchased := readDay(prompt);
end;
end;
procedure displayByDayPurchased(customers:TCustomers);
var
specific_day:integer;
begin
specific_day := ReadInteger('Enter day to see items purchased');
if (specific_day = customers.dayPurchased[specific_day])then
begin
end;
end;
procedure Main();
var
customer: Tcustomers;
begin
customer := readCustomers('Read in customers');
end;
begin
Main();
end.
my code doesn't seem to work, ive tried implementing the code within the displayByDayPurchased procedure.
好吧,在您发布的代码中,您的 displayByDayPurchased
实际上并没有实现任何会导致显示匹配记录的内容,您所拥有的只是一个空的 begin ... end
块:
if (specific_day = customers.dayPurchased[specific_day])then
begin
end;
和 a) 无论如何都没有正确指定匹配条件和 b) 它不会编译,因为 Tcustomers
数组没有 dayPurchased
字段( Tcustomer
记录有,但数组没有)。
你的代码依赖了很多你没有提供定义的东西(ReadString
,ReadInteger
,ReadIntegerRange
,等等)所以很难给你一个测试解决方案。
然而,您的 displayByDayPurchased
的实现可能看起来像这样:
procedure displayByDayPurchased(customers:TCustomers);
var
specific_day:integer;
i : integer;
ADay : Tday;
begin
specific_day := ReadInteger('Enter day to see items purchased');
ADay := Tday(specific_day); // converts integer to TDay value
for i := Low(customers) to High(Customers) do begin
if customers[i].dayPurchased = ADay then begin
writenln(customers[i].itemPurchased);
end;
end;
end;
我假设您的 Tcustomer
记录实际上包含客户的姓名,并且您的代码需要修改才能处理。
顺便说一句,你的 function readDay(prompt:string): Tday
是错误的;由于您对 Tday
的定义,readDay
中允许的值应该是 0 和 1,因为 Tday
枚举的最低值实际上对应于零,而不是 1。
此外,您没有说明您使用的是哪个 Pascal 编译器,但大多数现代版本都允许像 Tday(integerValue)
这样的调用将整数转换为枚举的实例值。