我在读出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但还是抛出异常
高手指点迷津
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但还是抛出异常
高手指点迷津
是不是这句的问题?
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;或者是你的循环导致越界访问抛出的异常!
改为:
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;