无论实际值如何,条件始终评估为真
Condition always evaluates to true regardless of actual values
我创建了一个 if
语句,当输入的代码与生成的代码匹配时,我想使用 `ShowMessage() 函数输出一条消息。但是,它仍然输出“干得好!”即使是明显错误的消息。
按钮代码:
procedure TfrmCAPTCHA.btnCheckClick(Sender: TObject);
var
sCode, sAnswer : string;
bRightAnswer, bWrongAnswer : boolean;
begin
{button to verify input}
sANswer := Trim(edtAnswer.Text);
sCode := edtCaptchaCode.Text;
bRightAnswer := (sAnswer = sCode);
bRightAnswer := True;
bWrongAnswer := (sAnswer <> sCode);
bWrongAnswer := False;
if bRightAnswer then
begin
btnCAPTCHAContinue.Show;
Showmessage('Nicely Done, seems you''''re all set to go!' + #13 +
'Press the continue button to collect your codes young wizzling');
end
else begin
edtCAPTCHACode.clear;
edtAnswer.clear;
btnCAPTCHAContinue.Hide;
Showmessage( 'That seems to be the wrong answer, please try again');
end;
end;
那是因为你先把bRightAnswer
设置成true
/false
看是否正确,然后你马上又用固定的true
覆盖了一遍不管条件。同样的事情在下一个块设置 bWrongAnswer
到 false
:
bRightAnswer := (sAnswer = sCode);
bRightAnswer := True; {regardless of what it was before, it's always true now}
bWrongAnswer := (sAnswer <> sCode);
bWrongAnswer := False; {regardless of what it was before, it's always false now}
删除这两行。
您可以通过将 bWrongAnswer := (sAnswer <> sCode);
替换为 bWrongAnswer := not bRightAnswer;
来进一步简化代码,或者更进一步并完全删除 bWrongAnswer
变量并仅在所有位置使用 not bRightAnswer
你的代码。
其实现在看来,反正你连bWrongAnswer
都用不着,直接去掉应该没什么坏处。
如果您愿意,也可以删除 bRightAnswer
并将 if bRightAnswer then
直接替换为 if sAnswer = sCode then
。
我创建了一个 if
语句,当输入的代码与生成的代码匹配时,我想使用 `ShowMessage() 函数输出一条消息。但是,它仍然输出“干得好!”即使是明显错误的消息。
按钮代码:
procedure TfrmCAPTCHA.btnCheckClick(Sender: TObject);
var
sCode, sAnswer : string;
bRightAnswer, bWrongAnswer : boolean;
begin
{button to verify input}
sANswer := Trim(edtAnswer.Text);
sCode := edtCaptchaCode.Text;
bRightAnswer := (sAnswer = sCode);
bRightAnswer := True;
bWrongAnswer := (sAnswer <> sCode);
bWrongAnswer := False;
if bRightAnswer then
begin
btnCAPTCHAContinue.Show;
Showmessage('Nicely Done, seems you''''re all set to go!' + #13 +
'Press the continue button to collect your codes young wizzling');
end
else begin
edtCAPTCHACode.clear;
edtAnswer.clear;
btnCAPTCHAContinue.Hide;
Showmessage( 'That seems to be the wrong answer, please try again');
end;
end;
那是因为你先把bRightAnswer
设置成true
/false
看是否正确,然后你马上又用固定的true
覆盖了一遍不管条件。同样的事情在下一个块设置 bWrongAnswer
到 false
:
bRightAnswer := (sAnswer = sCode);
bRightAnswer := True; {regardless of what it was before, it's always true now}
bWrongAnswer := (sAnswer <> sCode);
bWrongAnswer := False; {regardless of what it was before, it's always false now}
删除这两行。
您可以通过将 bWrongAnswer := (sAnswer <> sCode);
替换为 bWrongAnswer := not bRightAnswer;
来进一步简化代码,或者更进一步并完全删除 bWrongAnswer
变量并仅在所有位置使用 not bRightAnswer
你的代码。
其实现在看来,反正你连bWrongAnswer
都用不着,直接去掉应该没什么坏处。
如果您愿意,也可以删除 bRightAnswer
并将 if bRightAnswer then
直接替换为 if sAnswer = sCode then
。