Delphi 7 - 多个else如果没有显示正确的标签

问题描述 投票:0回答:2

我正在努力在Delphi 7中制作一个血压类别检查器,我刚认识了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.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.

这可能是一些常见的错误,但我仍然无法自己解决,任何形式的帮助都会有很大的帮助,谢谢。

delphi if-statement conditional delphi-7
2个回答
5
投票

您的代码不正确。它只检查两个值,即< 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;

(你的范围真的很混乱。你需要调整我的代码以满足你的实际范围要求,但我发布的内容应该让你开始。)

因为不太可能有人将血压记录为浮点值(不太可能你的BP将是121.6 / 97.2),你可能想要使用整数,这将使代码更容易。

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;

2
投票

IF声明有什么作用?它检查条件。如果条件是TRUE,它执行THEN子句。如果条件是FALSE,它会执行ELSE子句,如果有的话。

当你写一个IF-ELSE-IF-ELSE-IF-ELSE -...的序列时,没有什么特别的事情发生。测试了第一个IF的状况。如果它是TRUE,它执行THEN子句,如果不是它继续ELSE子句。 ELSE子句可以包含任何声明。它可以是FORWHILE或赋值或函数调用或BEGIN-END块或任何其他语句。在这种情况下,ELSE条款恰好是另一个IF语句,因此检查IF的条件,依此类推。

最后,您编写了检查许多条件的代码,并在第一个条件TRUE处停止。

选择一个值并手动跟踪代码以查看其工作原理。

a=100开始。第一个IF问是100<120。这是TRUE所以它将caption设置为'optimal'ELSE子句从未执行过,一切都很好。

a=200开始。第一个IF问是200<120。答案是否定的,所以执行传递给ELSE子句。 ELSE条款由IF组成,询问200>120。答案是肯定的,所以它将caption设置为'normal'。这不是你想要的。

考虑一下a=120。第一个IF尝试120<120。这是FALSE所以它继续ELSE条款。在那里我们发现IF问是120>120。这也是FALSE,所以我们通过到下一个IF。那个问题是120<130。这是TRUE所以caption设置为'normal'

实际上,这些示例显示了代码中的每条可能路径。没有达到过IF的范围。

这是一个应该按照您的意图工作的示例。

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;

<<=之间选择时要小心。当a正好是120时,想要发生什么?

小心考虑所有可能的值。当你编写原始代码时,你打算在a=180.5时发生什么。 <180有条件,>181有条件。从180181的数值被忽略了。

© www.soinside.com 2019 - 2024. All rights reserved.