gpt4 book ai didi

arrays - Delphi boolean 返回函数提示 : Value assigned to '' never used?

转载 作者:行者123 更新时间:2023-12-03 18:57:16 25 4
gpt4 key购买 nike

我有

playerIds : Array[0..500] of string;

function isPlayerMob(check : string): boolean;
var
i : integer;
begin
for i := 0 to 500 do
begin
if ((playerIds[i] <> '') and (playerIds[i] = check)) then
begin
result := true;
end;
end;
result := false;
end;

我收到警告

Hint: Value assigned to 'isPlayerMob' never used

谁能告诉我如何解决这个问题?错误是针对

result := true;

最佳答案

正如其他人告诉您的那样,您的循环分配给 Result 的值将被丢弃,因为您没有在最终分配给 Result 之前退出函数,所以它循环分配什么并不重要。

您可以为结果分配一个初始值,然后根据需要重新分配它,或者您可以在分配了所需的值后简单地退出:

function isPlayerMob(check : string): boolean;
var
i : integer;
begin
for i := 0 to 500 do
begin
if ((playerIds[i] <> '') and (playerIds[i] = check)) then
begin
Result := True;
Exit; // <-- add this
end;
end;
Result := False; // <-- only performed if the loop does not find a match
end;

或者,如果您使用的是最新的 Delphi 版本:

function isPlayerMob(check : string): boolean;
var
i : integer;
begin
for i := 0 to 500 do
begin
if ((playerIds[i] <> '') and (playerIds[i] = check)) then
Exit(True); // <-- sets Result and exits at the same time
end;
Result := False; // <-- only performed if the loop does not find a match
end;

关于arrays - Delphi boolean 返回函数提示 : Value assigned to '' never used?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com