Delphi 7-如果未显示正确的标签,则显示多个else
我正试图在Delphi7中制作一个血压分类检查器,我刚刚认识Delphi几个星期。问题是每次我把数字放在120以上,标签标题总是显示正常。 这是我的密码:Delphi 7-如果未显示正确的标签,则显示多个else,delphi,if-statement,conditional,delphi-7,Delphi,If Statement,Conditional,Delphi 7,我正试图在Delphi7中制作一个血压分类检查器,我刚刚认识Delphi几个星期。问题是每次我把数字放在120以上,标签标题总是显示正常。 这是我的密码: procedure TForm1.Button1Click(Sender: TObject); var a,b:real; begin a:=strtofloat(edit1.Text); if (a<120) then label1.caption:='optimal' else if (a>120) then label1.c
procedure TForm1.Button1Click(Sender: TObject);
var a,b:real;
begin
a:=strtofloat(edit1.Text);
if (a<120) then label1.caption:='optimal'
else if (a>120) then label1.caption:='normal'
else if (a<130) then label1.caption:='normal'
else if (a>130) then label1.caption:='normal high'
else if (a<140) then label1.caption:='normal high'
else if (a>140) then label1.caption:='grade 1 hypertension'
else if (a<160) then label1.caption:='grade 1 hypertension'
else if (a>160) then label1.caption:='grade 2 hypertension'
else if (a<180) then label1.caption:='grade 2 hypertension'
else if (a>181) then label1.caption:='grade 3 hypertension'
end;
end.
procedure TForm1.按钮1点击(发送方:TObject);
var a,b:真实值;
开始
a:=strtofloat(edit1.Text);
如果是(a120),则标签1.说明:='normal'
如果为(a130),则为标签1。说明:=‘正常高’
如果是(a140),则标签1。说明:='1级高血压'
如果是(a160),则标签1.说明:='2级高血压'
如果是(a181),则标签1.说明:='3级高血压'
结束;
结束。
这可能是一些常见的错误,但我自己仍然无法解决,任何帮助都会有很大帮助,谢谢。您的代码不正确。它只检查两个值,即
<120
和>120
。其他任何东西都没有经过测试
在查找范围内的值时,需要测试范围的两端,如下所示:
procedure TForm1.Button1Click(Sender: TObject);
var
a: real;
begin
a:=strtofloat(edit1.Text);
if (a < 120) then
Label1.Caption := 'Optimal'
else if (a >= 120) and (a < 130) then
Label1.Caption := 'Normal'
else if (a >= 130) and (a < 150) then
Label1.Caption := 'Normal high'
else if (a >= 150) and (a < 160) then
Label1.Caption := 'Grade 1 hypertension'
else if (a >= 160) and (a < 170) then
Label1.Caption := 'Grade 2 hypertension'
else if (a >= 170) and (a < 180) then
Label1.Caption := 'Grade 3 hypertension'
else
Label1.Caption := 'Heart exploded from pressure';
end;
IF
语句的作用是什么?它检查情况。如果条件为TRUE
,则执行THEN
-子句。如果条件为FALSE
,则执行ELSE
-子句(如果有)
当您编写一系列的IF
-ELSE
-IF
-ELSE
-IF
-ELSE
-。。。没什么特别的事。测试第一个的条件(如果)。如果是TRUE
,则执行THEN
-子句,如果不是,则继续执行ELSE
-子句。ELSE
-子句可以包含任何语句。它可以是FOR
或WHILE
或赋值或函数调用或BEGIN
-END
块或任何其他语句。在这种情况下,ELSE
-子句恰好是另一个IF
语句,因此检查了IF
的条件,依此类推
最后,您编写了检查许多条件的代码,并在第一个条件为TRUE
时停止
选择一个值并手动跟踪代码以查看其工作方式
从a=100开始。如果
询问的第一个是100如果您颠倒测试顺序,代码将正常工作:首先检查是否>180,然后检查是否>170,等等。使用case语句的可接受答案是干净的方法。使用integer和case,或者逆转测试,首先从a>181开始,因为150也大于120、130和140。。。因此,在您的logicTest(a>=…)中,您永远不会达到超过a>120的测试,因为下限由前面的if(a<…)-not(a<…)=(a>=…:)保证,如果a=120,那么将有第三个也是最后一个test@DavidHeffernan-据我所知,120<130:)。第一个测试(a<120)=false,第二个测试(a<130)=true,因此第二个测试是最终测试当我阅读问题时,我还考虑使用case
关键字。你先回答,把我揍了一顿。回答得好@Ken只是有点吹毛求疵,您在Optimal
和Grade 1高血压中缺少撇号。仅供参考,您的数值范围与我在此处找到的数值范围不匹配:
procedure TForm1.Button1Click(Sender: TObject);
var
a: Integer;
begin
a := StrToInt(Edit1.Text);
case a of
0..119: Label1.Caption := 'Optimal'; // Probably want to test for too low
120..129: Label1.Caption := 'Normal';
130..149: Label1.Caption := 'Normal high';
150..159: Label1.Caption := 'Grade 1 hypertension';
160..169: Label1.Caption := 'Grade 2 hypertension';
else
Label1.Caption := 'Over 170! Danger!'
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a, b : real;
begin
a := strtofloat(edit1.Text);
if a < 120 then label1.caption := 'optimal'
else if a < 130 then label1.caption := 'normal'
else if a < 140 then label1.caption := 'normal high'
else if a < 160 then label1.caption := 'grade 1 hypertension'
else if a < 180 then label1.caption := 'grade 2 hypertension'
else label1.caption := 'grade 3 hypertension'
end;