Pascal 布尔值 return 值?
Pascal Boolean return value?
我目前正在进行一项测试用户输入的布尔值的练习,如下所示:
function ReadBoolean(prompt: String): Boolean;
var
choice: String;
exit: boolean;
begin
repeat
begin
WriteLn(prompt);
ReadLn(choice);
case choice of
'yes','y','t','true': exit := true;
'no','n','f','false': exit := false;
else
WriteLn('Not a boolean input. Enter again: ');
end;
end;
until exit=true or exit=false;
result := exit;
end;
预计会一直循环询问值,直到它收到来自指定字符串的输入,但是在我第一次尝试输入 'fred' 时,布尔变量自动分配为 TRUE 并退出函数.
任何帮助将不胜感激。
您的循环在 exit=true or exit=false
时终止。由于 exit
只能是这两个值之一,它将始终满足该条件,因此它永远不会 运行 你的循环。
而且,考虑在开始循环之前显式设置 exit := false
的值。
据我了解,您只希望循环在用户输入某些特定字符串时结束。
可以这样修改until
条件来实现:
choice='yes' or choice='y' or choice='t' or choice='true' or choice='no' or choice='n' or choice='f' or choice='false'
或者,创建一个无限循环并在用户输入预期字符串时中断它:
while true do
...
'yes','y','t','true':
begin
exit := true;
break;
end;
'no','n','f','false':
begin
exit := false;
break;
end;
...
end;
您在这里问的是 "nullable" 布尔值(值为真,值为假,未提供值)。据我所知,它没有在任何 Pascal 方言中实现。因此,您必须将您的指示分成两个单独的标志:a) 是否有用户提供的任何格式正确的输入; b) 是输入被识别为真还是假
function ReadBoolean(prompt: String): Boolean;
var
choice: String;
exit: boolean;
recognized: boolean; { this is our termination flag }
begin
recognized := false; { we place it to false initially as no user input recognized yet }
repeat
begin
WriteLn(prompt);
ReadLn(choice);
case choice of
'yes','y','t','true': begin exit := true; recognized := true; end; { we mark it as recognized }
'no','n','f','false': begin exit := false; recognized := true; end; { we mark it as recognized }
else
WriteLn('Not a boolean input. Enter again: ');
end;
end;
until not recognized; { we keep asking for user input until known input provided }
result := exit;
end;
我目前正在进行一项测试用户输入的布尔值的练习,如下所示:
function ReadBoolean(prompt: String): Boolean;
var
choice: String;
exit: boolean;
begin
repeat
begin
WriteLn(prompt);
ReadLn(choice);
case choice of
'yes','y','t','true': exit := true;
'no','n','f','false': exit := false;
else
WriteLn('Not a boolean input. Enter again: ');
end;
end;
until exit=true or exit=false;
result := exit;
end;
预计会一直循环询问值,直到它收到来自指定字符串的输入,但是在我第一次尝试输入 'fred' 时,布尔变量自动分配为 TRUE 并退出函数.
任何帮助将不胜感激。
您的循环在 exit=true or exit=false
时终止。由于 exit
只能是这两个值之一,它将始终满足该条件,因此它永远不会 运行 你的循环。
而且,考虑在开始循环之前显式设置 exit := false
的值。
据我了解,您只希望循环在用户输入某些特定字符串时结束。
可以这样修改until
条件来实现:
choice='yes' or choice='y' or choice='t' or choice='true' or choice='no' or choice='n' or choice='f' or choice='false'
或者,创建一个无限循环并在用户输入预期字符串时中断它:
while true do
...
'yes','y','t','true':
begin
exit := true;
break;
end;
'no','n','f','false':
begin
exit := false;
break;
end;
...
end;
您在这里问的是 "nullable" 布尔值(值为真,值为假,未提供值)。据我所知,它没有在任何 Pascal 方言中实现。因此,您必须将您的指示分成两个单独的标志:a) 是否有用户提供的任何格式正确的输入; b) 是输入被识别为真还是假
function ReadBoolean(prompt: String): Boolean;
var
choice: String;
exit: boolean;
recognized: boolean; { this is our termination flag }
begin
recognized := false; { we place it to false initially as no user input recognized yet }
repeat
begin
WriteLn(prompt);
ReadLn(choice);
case choice of
'yes','y','t','true': begin exit := true; recognized := true; end; { we mark it as recognized }
'no','n','f','false': begin exit := false; recognized := true; end; { we mark it as recognized }
else
WriteLn('Not a boolean input. Enter again: ');
end;
end;
until not recognized; { we keep asking for user input until known input provided }
result := exit;
end;