我试图找出正确的方法来传递一个使用Free Pascal case语句的例子到一个简单的if语句。
用例将是
begin usingCaseStatements;
var
user_age : Integer;
begin
Writeln('Please enter your age');
Readln(user_age);
case user_age of
1..12 : Writeln('You are too young');
else
Writeln('Invalid input');
end;
Writeln('Please any key to terminate the program');
Readln();
end.
- 使用if语句-
begin usingCaseStatements;
var
user_age : Integer;
begin
Writeln('Please enter your age');
Readln(user_age);
if user_age in 1..12 then
Writeln('You are too young')
else
Writeln('Invalid input');
Writeln('Please any key to continue');
Readln();
end.
我试过替换if语句片段中的“in”,但没有任何运气,我试过做if (user_age = 1..12) then
,它只给了我一个错误,编译器说语句正在等待“)”,但它发现..相反。我是非常新的FPC,所以帮助和提示将不胜感激。
4条答案
按热度按时间whlutmcx1#
大多数PC Pascals只支持最多256个元素的设置大小,所以从长远来看,josifoski推荐的解决方案更实用。
f2uvfpb92#
如果(
user_age >=1
)和(user_age <=12
),则anhgbhbe3#
语句
if user_age in 1..12 then
非常像Ada语法。Free Pascal是比较接近的:if user_age in [1..12] then
下面是一个简单的可编译示例:
输出量:
iqjalb3h4#
只是为了好玩。它在FPC 2.7.1上工作,但我不知道它是否会在稳定的2.6.4上工作
输出量: