我想检查多个备忘录中的特定字符串,如果全部都签出,然后运行一个过程,但是在我的代码中,该过程有时会运行,有时却不会,有时仅当一些已经签出时才运行。
这是我的代码:
procedure TForm1.Timer14Timer(Sender: TObject);
begin
if (pos('ActiveTunnel',memo10.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo10.text)<>0)
and (pos('ActiveTunnel',memo9.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo9.text)<>0)
and (pos('ActiveTunnel',memo8.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo8.text)<>0)
and (pos('ActiveTunnel',memo7.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo7.text)<>0)
and (pos('ActiveTunnel',memo6.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo6.text)<>0)
and (pos('ActiveTunnel',memo5.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo5.text)<>0)
and (pos('ActiveTunnel',memo4.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo4.text)<>0)
and (pos('ActiveTunnel',memo3.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo3.text)<>0)
and (pos('ActiveTunnel',memo2.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo2.text)<>0)
and (pos('ActiveTunnel',memo1.Text)<>0) or (pos('https://ipfounder.net/?sponsor',memo1.text)<>0)
then
begin
if Checkbox1.Checked = true then
begin
starttun;
sleep(3000);
routesaddlast;
end;
end;
end;
答案 0 :(得分:5)
如果您查看this table,将会发现and
的优先级高于or
。这意味着代码中的子句实际上等同于:
if (pos(..., memo10.Text) <> 0)
or ((pos(..., memo10.Text) <> 0) and (pos(..., memo9.Text) <> 0))
or ((pos(..., memo9.Text) <> 0) and (pos(..., memo8.Text) <> 0))
or ((pos(..., memo8.Text) <> 0) and (pos(..., memo7.Text) <> 0))
etc...
这就是为什么您得到有时它可以按预期工作,有时却不起作用的原因。我猜你实际上是想要的:
if ( (pos(..., memo10.Text) <> 0) or (pos(..., memo10.text) <> 0) ) and
( (pos(..., memo9.Text) <> 0) or (pos(..., memo9.text) <> 0) ) and
( (pos(..., memo8.Text) <> 0) or (pos(..., memo8.text) <> 0) ) and
etc...
换句话说,在句号前后加上括号,使它们的优先级高于和。
请注意,so you don't have to repeat yourself,您可以执行以下操作:
const
S0 = 'ActiveTunnel';
S1 = 'https://ipfounder.net/?sponsor';
procedure TForm1.Timer14Timer(Sender: TObject);
begin
if ((Pos(S0, memo10.Text) <> 0) or (Pos(S1, memo10.Text) <> 0)) and
((Pos(S0, memo9.Text) <> 0) or (Pos(S1, memo9.Text) <> 0)) and
// etc...
并对此进行简化:
function FindIt(memo: TMemo): Boolean;
begin
Result := (Pos(S0, memo.Text) <> 0) or (Pos(S1, memo.Text) <> 0);
end;
procedure TForm1.Timer14Timer(Sender: TObject);
begin
if FindIt(memo10) and
FindIt(memo9) and
FindIt(memo8) // etc.
当然,您还可以给 FindIt 一个开放数组参数,并传递任意数量的字符串,以使其更通用。