我在读出stringgrid中的值时候:
try
for j:=1 to n do
  begin
     b[j,1]:=strtofloat(stringgrid1.Cells[j,1]);  //用b[j,1]存放各状态概率
     if (0<=b[j,1]) and ( b[j,1]<=1)  then  h:=h+b[j,1];
  end;
  if h<>1 then
    raise Exception.Create('状态概率的和不等于1!');
except
on e:Exception do ShowMessage(E.Message);
end;
如果n=10,也就是stringgrid1为11列的时候
计算出来的h明明等于1但还是抛出异常
高手指点迷津

解决方案 »

  1.   

    for j:=1 to n do--->for j:=0 to n do  ?
      

  2.   

    if (0<=b[j,1]) and ( b[j,1]<=1)  then  h:=h+b[j,1];
    是不是这句的问题?
      

  3.   

    h=1的时候抛出的异常并非是你自己抛出的异常,也许是其余的代码抛出的异常。例如b[j,1]:=strtofloat(stringgrid1.Cells[j,1]); 这一行抛出的异常,又或者你的循环导致越界访问抛出的异常
      

  4.   

    改为如下:
    try
      n := Stringgrid1.Cells.count ;
      for j:=0 to n-1 do             //记得一定要从零开始  ,n=n-1! OK!
      begin
         b[j,1]:=strtofloat(stringgrid1.Cells[j,1]);  //用b[j,1]存放各状态概率
         if (0<=b[j,1]) and ( b[j,1]<=1)  then  h:=h+b[j,1];
      end;
      if h<>1 then
        raise Exception.Create('状态概率的和不等于1!');
    except
    on e:Exception do ShowMessage(E.Message);
    end;或者是你的循环导致越界访问抛出的异常!
      

  5.   

    该决了:
    改为:
    try
    for j:=1 to n do
      begin
         b[j,1]:=strtofloat(stringgrid1.Cells[j,1]);  //用b[j,1]存放各状态概率
         if (0<=b[j,1]) and ( b[j,1]<=1)  then  h:=h+b[j,1]*10;
      end;
      if h<>10 then
        raise Exception.Create('状态概率的和不等于1!');
    except
    on e:Exception do ShowMessage(E.Message);
    end;