'if'语句中的代码行未运行

时间:2014-02-21 21:27:17

标签: delphi if-statement

我一直遇到这个代码没有按照预期运行的主要问题。设置贵重物品PTIDStart和PTIDEnd的行都不会运行。

repeat
  if frmManagement.ADOTPlayers['TournamentID'] = TournamentID
  then
    PTIDStart := frmManagement.ADOTPlayers['PlayerTournamentID']
  else
    frmManagement.ADOTPlayers.Next;
until frmManagement.ADOTPlayers['TournamentID'] = TournamentID;

repeat
  if frmManagement.ADOTPlayers['TournamentID'] <> TournamentID 
  then
    PTIDEnd := frmManagement.ADOTPlayers['PlayerTournamanetID']
  else
    frmManagement.ADOTPlayers.Next;
until frmManagement.ADOTPlayers['TournamentID'] <> TournamentID;

请帮忙!我迫不及待地想解决这个问题。如果你想要现场演示修复那么请加我skype:ikyatt。

感谢。

编辑:如果有任何想法如何解决此问题,请发布以下修改后的代码。

3 个答案:

答案 0 :(得分:1)

您的if条件与它在同一循环中的until条件相同。因此,当条件满足时,执行将从重复之后的语句继续执行,直到if在满足条件时永远不会有机会被评估。

要解决,请在循环外移动if

repeat
  frmManagement.ADOTPlayers.Next;
until frmManagement.ADOTPlayers['TournamentID'] = TournamentID;
if frmManagement.ADOTPlayers['TournamentID'] = TournamentID
then
  PTIDStart := frmManagement.ADOTPlayers['PlayerTournamentID'];
...

对于您稍后可能遇到的其他问题,请参阅问题的评论。

答案 1 :(得分:1)

尝试更像这样的事情:

while not frmManagement.ADOTPlayers.Eof do
begin
  if frmManagement.ADOTPlayers['TournamentID'] = TournamentID then
  begin
    PTIDStart := frmManagement.ADOTPlayers['PlayerTournamentID']
    frmManagement.ADOTPlayers.Next;
    while not frmManagement.ADOTPlayers.Eof do
    begin
      if frmManagement.ADOTPlayers['TournamentID'] <> TournamentID then
      begin
        PTIDEnd := frmManagement.ADOTPlayers['PlayerTournamanetID'];
        Break;
      end;
      frmManagement.ADOTPlayers.Next;
    end;
    Break;
  end;
  frmManagement.ADOTPlayers.Next;
end;

或者这个:

PTIDStart := '';
PTIDEnd := '';

while frmManagement.ADOTPlayers.Eof do
begin
  if PTIDStart = '' then
  begin
    if frmManagement.ADOTPlayers['TournamentID'] = TournamentID then
      PTIDStart := frmManagement.ADOTPlayers['PlayerTournamentID'];
  end
  else if frmManagement.ADOTPlayers['TournamentID'] <> TournamentID then
  begin
    PTIDEnd := frmManagement.ADOTPlayers['PlayerTournamentID'];
    Break;
  end;
  frmManagement.ADOTPlayers.Next;
end;

答案 2 :(得分:0)

while frmManagement.ADOTPlayers['TournamentID'] <> TournamentID do
  frmManagement.ADOTPlayers.Next;
PTIDStart := frmManagement.ADOTPlayers['PlayerTournamentID']
while frmManagement.ADOTPlayers['TournamentID'] = TournamentID do
  frmManagement.ADOTPlayers.Next;
PTIDEnd := frmManagement.ADOTPlayers['PlayerTournamentID']

应该将Start设置为第一个匹配的TournamentID,并结束到之后的第一个NOT匹配。

如果你想在End中获得最后一个匹配,那么

while frmManagement.ADOTPlayers['TournamentID'] = TournamentID do
begin
  PTIDEnd := frmManagement.ADOTPlayers['PlayerTournamentID']
  frmManagement.ADOTPlayers.Next;
end;

repeat结构的问题在于,在您下次阅读之前,您的退出条件正在发生 - 在您录制之前。

repeat
  if frmManagement.ADOTPlayers['TournamentID'] <> TournamentID
    frmManagement.ADOTPlayers.Next;
until frmManagement.ADOTPlayers['TournamentID'] = TournamentID;
PTIDStart := frmManagement.ADOTPlayers['PlayerTournamentID']

repeat
  PTIDEnd := frmManagement.ADOTPlayers['PlayerTournamanetID'] // If you want "End=last"
  if frmManagement.ADOTPlayers['TournamentID'] = TournamentID 
  then
    frmManagement.ADOTPlayers.Next;
until frmManagement.ADOTPlayers['TournamentID'] <> TournamentID;
PTIDEnd := frmManagement.ADOTPlayers['PlayerTournamanetID'] // If you want "End=first non-match"

...如果您坚持使用repeat..until