program
Enumeration;
uses
crt;
type
beverage = (coffee, tea, milk, water, coke, limejuice);
var
drink:beverage;
begin`enter code here`
writeln('Which drink do you want?');
writeln('You have ', *** , ' choices');
readkey;
end.
我应该在' ***'这里得到输出中饮料枚举类型中的元素数量?
我使用sizeof()
,但它给出了类型的字节值。
我正在使用lazarus,fyi。
答案 0 :(得分:3)
Succ(Ord(High(beverage)))应该为您提供已定义饮料的数量。
答案 1 :(得分:2)
您可以使用 ORD 和 HIGH-LOW 功能:
ord(high(beverage)) - ord(low(beverage)) + 1
代码:
program
Enumeration;
uses
crt;
type
beverage = (coffee, tea, milk, water, coke, limejuice);
var
drink:beverage;
begin`enter code here`
writeln('Which drink do you want?');
writeln('You have ', ord(high(beverage))-ord(low(beverage))+1 , ' choices');
readkey;
end.
结果:
Which drink do you want?
You have 6 choices