Delphi if else if else 语句不起作用 "Type of expression must be BOOLEAN"

标签 delphi if-statement conditional-statements delphi-xe2 pascal

我正在尝试用 Delphi 制作一个货币转换器,自从我上次使用 Delphi 以来已经有一段时间了,所以我有点生疏了。当我尝试创建 if、else if、else 语句时,它给出错误:“表达式类型必须是 BOOLEAN”。

这是我的代码:

if Edit1.Text = '' And Edit2.Text <> ''
    then Edit2.Text := '1'
else
if Edit1.Text <> '' And Edit2.Text = ''
    then ShowMessage('Blah')
else
if Edit1.Text ='' And Edit2.Text = ''
    then ShowMessage('Please Enter A Value')
else
    ShowMessage('Mathing Suff...');

如果有人能看到我的愚蠢错误或出了什么问题,那将会有很大帮助。 :)

编辑:错误出现在第一个 if 语句及其后面的两个 else if 的行上。

最佳答案

这是因为运算符优先级,您应该将每个条件放在括号中

试试这个代码

if (Edit1.Text = '') And (Edit2.Text <> '')  then 
  Edit2.Text := '1'
else 
if (Edit1.Text <> '') And (Edit2.Text = '') then 
  ShowMessage('Blah')
else 
if (Edit1.Text ='') And (Edit2.Text = '')then 
  ShowMessage('Please Enter A Value')
else 
  ShowMessage('Mathing Suff...');

关于Delphi if else if else 语句不起作用 "Type of expression must be BOOLEAN",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17055816/

相关文章:

delphi - 如何在无需先绘制的情况下获得位图透明度?

xml - 解析 XML 数据绑定(bind)向导时值为空

Python 字典与 If 语句速度

c++ - 如果语句总是被执行

Python,与条件数组相反

ruby - 使用 ruby​​ 的 'case'/'when' 方法与 ||或 && 运算符等

sql-server - 使用 FireDac 在 Delphi 中动态创建和调用存储过程的正确方法是什么?

c# - 使用 if 语句缩短 for 循环 - C#

MySQL 根据条件每行更新值行

delphi - Win32 : Reporting technology that doesn't have to be installed or shipped