2013-03-12 48 views
0

我遇到的問題是我試圖比較fireShot()命令的結果'Splash'。接下來我需要計算總真值(或每次fireShot()='Splash')。然而,每次我嘗試使用while循環或if循環時,我最終會得到一個常數(與它們各自的圖中相應的隨機數或我的上一個函數不匹配)。我相信這樣的格式不太好,可以使用工作,但我相信真正的問題在於strcmp()的值的統計。如何使用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 

我知道我的代碼可能會關閉,但我所有的功能,達到此功能工作像他們應該。

+0

你從fireShot得到了什麼樣的字符串?另外,如果你使用strcmp,它將做一個區分大小寫的字符串匹配,如果你想不區分大小寫,那麼使用strcmpi – alrikai 2013-03-12 00:49:16

+0

以及fireShot調用一個隨機變量函數,該函數生成2個繪製在圖上的點。如果生成的2個點位於半徑爲1的圓圈(0,0)中,那麼如果它們在半徑之外,我會返回「飛濺」,從而得到「thud」。然而;我在這裏定義了Splash to = fireShot,因爲我試圖嘗試使用我在網上發現的東西,通過strcpm()將輸入變量與yes或no問題相關聯。我想,因爲我只有2個回報可供選擇,我可以嘗試使用類似的編碼,但可能使它更加混亂。 – Mrf442 2013-03-12 00:58:54

+0

我的問題是我相信strcmp(Splash,'Splash')的作品,但我無法弄清楚如何計算真正的價值。我們在命令窗口中調用函數,它給了我一個所有結果的列表(Splash或Thud)繪製圖上的點,但是當Land變量出現在結尾時= 2。我猜測1 strcmp是真實的)+1從if與我的土地=土地+ 1 – Mrf442 2013-03-12 01:00:37

回答

0

然後你的問題實際上只是一個邏輯錯誤; strcmp檢查沒問題,你只需要移動你的while循環中的你的Land統計邏輯就可以得到整個鏡頭的總數。實際情況是,當您執行檢查並在while循環之外進行增量時,最多隻能獲取+1到您的Land變量,具體取決於最後的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 
+0

我非常感謝你的幫助! – Mrf442 2013-03-12 01:29:16

+0

我唯一的問題是Land變量是未定義的。我究竟可以如何定義這一點。我之前將它設置爲strcmp(Splash,'Splash')函數,但是如果將它與If語句一起使用,我認爲我不能再這樣做。 – Mrf442 2013-03-12 01:43:24

+0

@MikeFritz最初將它設置爲0,即當你第一次輸入函數時(假設你以0飛濺開始) – alrikai 2013-03-12 01:46:34

相關問題