怎么样,如果shape1.brush.color:= clred和shape2.brush.color:= clred那么DO SOMETHING?

use*_*883 0 delphi if-statement brush colors shape

当我使用这两个代码运行应用程序时,显示相同的错误:

运算符不适用于此操作数类型

procedure TForm1.Button4Click(Sender: TObject);
begin
  If (shape1.Brush.Color:=clblue and shape2.Brush.Color:=clblue) then
  begin
    showMessage('error');
  end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  If (shape1.Brush.Color:=clblue) and (shape2.Brush.Color:=clblue) then
  begin
    showMessage('error');
  end;
Run Code Online (Sandbox Code Playgroud)

Mas*_*ler 6

这里有两个问题.

首先,正在使用的运算符:=是赋值,而不是相等性检查.为此,你想要=.

其次,由于andor运算符的优先级问题,同一表达式中的多重比较需要围绕每个单独的比较进行括号.所以你想要的是:

if (shape1.Brush.Color = clblue) and (shape2.Brush.Color = clblue) then
begin
  showMessage('error');
end;
Run Code Online (Sandbox Code Playgroud)

  • @FreeConsulting:根据[Delphi语言指南](http://docwiki.embarcadero.com/RADStudio/XE5/en/Declarations_and_Statements#Assignment_Statements):"::=符号有时称为赋值运算符." 根据该页面,梅森在使用短语时是正确的.(在我看来,你对语义的争论对新用户没有帮助,这显然适用于这个问题的海报.为什么通过在评论中讨论一些挑剔的细节来混淆梅森的答案?) (4认同)
  • @Free发明你自己的私人术语然后责备别人不遵守那个术语,这是相当小而无意义的. (3认同)