在delphi7中选择结构为if then else但只能把条件分为两个条件,有没有选择结构可以把条件分为多个条件,
如 if a<=10 then
begin
end
else
begin
if a>10 then
....
else
....
end
有没有类似sql中case when ...then...
when ...then...
when ...then...
when ...then...
when ...then...
else
.....
end;
如 if a<=10 then
begin
end
else
begin
if a>10 then
....
else
....
end
有没有类似sql中case when ...then...
when ...then...
when ...then...
when ...then...
when ...then...
else
.....
end;
s : integer;
case s of
1 : Color := clRed;
2 : Color := clYellow;
3 : Color := clBlue;
end;
例子一:case I of 1..5: Caption := 'Low';
6..9: Caption := 'High';
0, 10..99: Caption := 'Out of range';
else
Caption := '';
end;这相当于下面:if I in [1..5] then Caption := 'Low'
else if I in [6..10] then
Caption := 'High'
else if (I = 0) or (I in [10..99]) then
Caption := 'Out of range'
else
Caption := '';另外的例子case MyColor of Red: X := 1;
Green: X := 2;
Blue: X := 3;
Yellow, Orange, Black: X := 0;
end;case Selection of Done: Form1.Close;
Compute: CalculateTotal(UnitCost, Quantity);
else
Beep;
end;
beginend
else if a > 9 then
beginend
else if a > 8 then
beginend
...
const
clRed = 1;
clYellow = 2;
clBlue = 3;
string is invalid.
字符型是可以的
case s of 中的S必须为ordinal type,而string types是不行的
字符型如下:var s:char;
begin
s:='3';
case s of
'1':showmessage(s);
'2':showmessage(s);
else
showmessage(s);
end;
end;
也可以使用case ... of语句进行多条件判断,
当然,所判断的数据类型需要根据实际的类型进行赋值!
下面的代码用case 怎样写?
if dw='包装一' then...
else
begin
if dw='包装二' then ...
else
...
end;
在delphi帮助文档中你可以找到一段话
case selectorExpression of
caseList1: statement1;
...
caseListn: statementn;
endwhere selectorExpression is any expression of an ordinal type (string types are invalid)
可见string类型是不可以的
就用if else不就可以了吗?能达到目的即可。
这是方法
strList.Add('Beijing');
strList.Add('Tianjing');
strList.Add('Shanghai');
strList.Add('Chongqing');
str := 'Chongqing';
case strList.IndexOf(str) of
0: ShowMessage('First');
1: ShowMessage('Second');
2: ShowMessage('Third');
3: ShowMessage('Forth');
else ShowMessage('Other');
end;
strList.Free;
1:
2:
.
.
.
end;
注意,X只接受整形,列举,还有一个忘记了,不接受字符串
难道你要说这也是c/c++的bug吗?
而且delphi本身也没说自己的case of就支持string类型,如果它自称可以支持string类型但没有实现,那就叫bug。
查了一下资料,c#支持int,char和string,但是这也只能说c#在这点上有所改进,却不能说c,c++,delphi在条件语句中有不支持string的bug.
使用 if... then else if... then 也同样可以的。