我在下面的代碼中收到此錯誤:
下限超過上限
如果你交換它們,你會得到一個不同的錯誤:
重復的案例標簽
我能做些什么?
我已經嘗試了所有沒有Int64()和使用的方法Int64(),但沒有幫助。
自己試試吧,不管你輸入不輸入,錯誤都會存在。
下面有幾種方法可以做到這一點,但我想知道為什么這段代碼會出錯:
Delphi 不能在下面使用,因為它不能實作對 case 陳述句的 64 位處理。
function cntCaseTbl(Z: Int64): Int64;
begin
case Abs(Z) of
0..9: Exit(1);
10..99: Exit(2);
100..999: Exit(3);
1000..9999: Exit(4);
10000..99999: Exit(5);
100000..999999: Exit(6);
1000000..9999999: Exit(7);
10000000..99999999: Exit(8);
100000000..999999999: Exit(9);
1000000000..9999999999: Exit(10);
10000000000..99999999999: Exit(11);
100000000000..999999999999: Exit(12);
1000000000000..9999999999999: Exit(13);
10000000000000..99999999999999: Exit(14);
100000000000000..999999999999999: Exit(15);
1000000000000000..9999999999999999: Exit(16);
10000000000000000..99999999999999999: Exit(17);
100000000000000000..999999999999999999: Exit(18);
1000000000000000000..9223372036854775807: Exit(19);
end;
end;
uj5u.com熱心網友回復:
在第 131 行,10000000000并且99999999999是Integers 被型別轉換為Int64,并且它們都在 的范圍之外Integer,因此它們在被轉換之前溢位,從而導致第一個值小于第二個值,因此出現錯誤。
其他行也一樣。
這在 Delphi 的檔案中有所描述:
案例陳述
case 陳述句可以為深度嵌套的 if 條件提供一個可讀的替代方案。案例陳述具有以下形式:
case selectorExpression of caseList1: statement1; ... caseListn: statementn; end其中selectorExpression是任何小于 32 位的序數型別的運算式(字串型別和大于 32 位的序數是無效的)...
我建議完全擺脫它,case而只使用回圈,例如:
function cntCaseTbl(Z: Int64): Int64;
begin
Result := 0;
Z := Abs(Z);
while Z > 0 do begin
Inc(Result);
Z := Z div 10;
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503922.html
