如何使用带有多个参数的while循环?

时间:2021-04-30 20:10:47

I want make a countdown timer in Delphi. I used

我想在Delphi中制作一个倒数计时器。我用了

while(t<>0 and m<>0 and s<>0) do

but the program doesn't accept it. How can I fix this?

但该计划不接受它。我怎样才能解决这个问题?

2 个解决方案

#1


The code you need is:

您需要的代码是:

while (t<>0) and (m<>0) and (s<>0) do

The reason is that Pascal precedence rules mean that in your code, and attempts to bind as a bitwise operator to the integers around it. The parens above are needed to make and operate here as a logical operator.

原因是Pascal优先级规则意味着在您的代码中,并尝试将其作为按位运算符绑定到它周围的整数。上面的parens需要在这里作为逻辑运算符进行操作。

Your expression is

你的表达是

t<>0 and m<>0 and s<>0

Because and has a higher precedence than <>, then the expression is interpreted as:

因为并且优先级高于<>,所以表达式被解释为:

t<>(0 and m)<>(0 and s)<>0

which is a clear syntax error.

这是一个明确的语法错误。

Precedence is documented here:

这里记录了优先顺序:

Expressions (Delphi) | Operator Precedence

表达式(德尔福)|运营商优先权

#2


You don't need loops at all inside your function. This is because you are working from the timer which calls once per time interval.

你的功能中根本不需要循环。这是因为您正在使用每个时间间隔调用一次的计时器。

You simply need to decrement the variables like this

你只需要递减这样的变量

procedure TForm1.Timer1Timer(Sender: TObject) 
begin 
  Label1.Caption:=IntToStr(t)+':'+IntToStr(m)+':'+IntToStr(s); 
  if s > 0 then dec( s)
  else
  begin
    s := 59;
    if m>0 then dec(m)
    else
    begin
      m := 59;
      if t>0 then dec( t )
      else Label2.Caption:='it is done' 
    end;
  end;
end;

#1


The code you need is:

您需要的代码是:

while (t<>0) and (m<>0) and (s<>0) do

The reason is that Pascal precedence rules mean that in your code, and attempts to bind as a bitwise operator to the integers around it. The parens above are needed to make and operate here as a logical operator.

原因是Pascal优先级规则意味着在您的代码中,并尝试将其作为按位运算符绑定到它周围的整数。上面的parens需要在这里作为逻辑运算符进行操作。

Your expression is

你的表达是

t<>0 and m<>0 and s<>0

Because and has a higher precedence than <>, then the expression is interpreted as:

因为并且优先级高于<>,所以表达式被解释为:

t<>(0 and m)<>(0 and s)<>0

which is a clear syntax error.

这是一个明确的语法错误。

Precedence is documented here:

这里记录了优先顺序:

Expressions (Delphi) | Operator Precedence

表达式(德尔福)|运营商优先权

#2


You don't need loops at all inside your function. This is because you are working from the timer which calls once per time interval.

你的功能中根本不需要循环。这是因为您正在使用每个时间间隔调用一次的计时器。

You simply need to decrement the variables like this

你只需要递减这样的变量

procedure TForm1.Timer1Timer(Sender: TObject) 
begin 
  Label1.Caption:=IntToStr(t)+':'+IntToStr(m)+':'+IntToStr(s); 
  if s > 0 then dec( s)
  else
  begin
    s := 59;
    if m>0 then dec(m)
    else
    begin
      m := 59;
      if t>0 then dec( t )
      else Label2.Caption:='it is done' 
    end;
  end;
end;