簡體   English   中英

如何使用strcmp()將函數的返回值與循環中的函數調用進行比較?

[英]How to use strcmp() to compare a returned value of a function to a function call in a loop?

我遇到的問題是我試圖將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

我知道我的代碼可能已關閉,但是直到該功能的所有功能都按預期運行。

那么您的問題實際上只是一個邏輯錯誤。 strcmp檢查很好,您只需要在while循環內移動Land計數邏輯即可獲得“ Splash”的總射擊次數。 strcmp原樣,當您執行檢查並在while循環之外進行遞增操作時,您最多將對Land變量+1,具體取決於最后一個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);

        %// If the ball hits inside the pond
        %// Land (total tally) gets +1
        if (strcmp(Splash,'Splash'))
            Land = Land + 1;
        end
    end      
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM