0

我遇到的问题是我试图将 fireShot() 命令的结果与结果“Splash”进行比较。接下来我需要计算总的真实值(或每次 fireShot()= 'Splash')。但是,每次我尝试使用 while 循环或 if 循环时,我都会得到一个常数(与相应的随机数或我在各自绘图上的前面的函数不匹配)。我确信它的格式不是很讨人喜欢并且可以使用工作,但我相信我真正的问题在于 strcmp() 的值的统计。

无论如何,这是我的代码:

function [ output_args ] = fireShots(shotstofire, fieldSize, pondRadius)
%// This function will fire multiple shots and count how many of them     
%// land in the pond.

    %// Counts down total number of shots and subtracts 1 until
    %// total is equal to '0'    
    while shotstofire > 0
        shotstofire;
        shotstofire = shotstofire - 1;

        %// Calls fireShot function
        Splash = fireShot(fieldSize,pondRadius);
        Land = strcmp(Splash,'Splash');
    end

    %// If the ball hits inside the pond
    %// Land (total tally) gets +1
    if (Land)
        Land = Land + 1;
    else

    end        
end

我知道我的代码可能已关闭,但我的所有功能都可以按预期工作。

4

1 回答 1

0

那么你的问题其实只是逻辑错误;strcmp 检查很好,您只需将您的Land计数逻辑移动到您的 while 循环中即可获得“Splash”的总镜头数。Land实际上,根据 last 的结果strcmp,当您在 while 循环之外执行检查和递增时,您最多会为您的变量获得 +1 。

function [ output_args ] = fireShots(shotstofire, fieldSize, pondRadius)
    %// This function will fire multiple shots and count how many of them     
    %// land in the pond.

    %// Counts down total number of shots and subtracts 1 until
    %// total is equal to '0'    
    while shotstofire > 0
        shotstofire;
        shotstofire = shotstofire - 1;

        %// Calls fireShot function
        Splash = fireShot(fieldSize,pondRadius);

        %// If the ball hits inside the pond
        %// Land (total tally) gets +1
        if (strcmp(Splash,'Splash'))
            Land = Land + 1;
        end
    end      
end
于 2013-03-12T01:11:23.493 回答