2014-10-01 45 views
-1

我想在一排中找到匹配的硬幣面。我設法讓一行中的3個匹配的尾巴結束循環。如何在一排中找到匹配的硬幣?

但我怎麼能包括頭(它似乎忽略頭)?

例如:

0 is heads 
reset 
0 is heads 
reset 
1 is tails 
1 is tails 
1 is tails 
total flips took is: 5 

PHP:

$flipCounts = 0; 
$matchingFaceTypes = 0; 

$targetReached = false; 
while ($matchingFaceTypes < 3) { 
    $faceType = rand(0, 1); 
    $flipCounts++; 
    if($faceType == 0) { 
     $matchingFaceTypes++; 
     echo $faceType . " is heads ". "\n"; 
     } 
    if($faceType == 1) { 

     $matchingFaceTypes++; 
     echo $faceType." is tails ". "\n"; 

    } else { 
      $matchingFaceTypes =0; 
      echo " reset ". "\n"; 
     } 
    } echo "total flips took is: " . $flipCounts; 
+0

你設置'$ matchingFaceTypes = 0;'任何時候'$ faceType == 1'都是錯誤的。你想要做的是跟蹤以前的類型是什麼,然後重置,如果當前類型不同於以前的類型。 – 2014-10-01 17:26:24

回答

2
$maxMatches = 3; 
$matches = array('tails' => 0, 'heads' => 0, 'total' => 0); 

while(max($matches['tails'], $matches['heads']) < $maxMatches) { 
    $faceType = rand(0, 1); 
    if ($faceType) { 
     $matches['heads']++; 
     $matches['tails'] = 0; 
     echo $faceType . " is heads\n"; 
    } 
    else { 
     $matches['tails']++; 
     $matches['heads'] = 0; 
     echo $faceType . " is tails\n"; 
    } 
    $matches['total']++; 
} 

echo "total flips took is: " . $matches['total']; 

說到最大的(

$maxMatches = 3; 
$total = 0; 
$matches = array('tails' => 0, 'heads' => 0); 

while(max($matches) < $maxMatches) { 
    $faceType = rand(0, 1); 
    if ($faceType) { 
     $matches['heads']++; 
     $matches['tails'] = 0; 
     echo $faceType . " is heads\n"; 
    } 
    else { 
     $matches['tails']++; 
     $matches['heads'] = 0; 
     echo $faceType . " is tails\n"; 
    } 
    $total++; 
} 

echo "total flips took is: " . $total; 
+0

這工作,謝謝。我想問,是否有必要在while參數中使用max? – Martynogea 2014-10-01 17:46:54

+0

@Martynogea你可以分別檢查每個元素的'head'和'tail'。我的方式只是略短。如果你將'total'移動到一個單獨的變量上,那麼你可以只做'max($ matches)<$ maxMatches' – Cheery 2014-10-01 17:49:03

+0

@Martynogea我寫了另一個例子,其中'max()'是有用的。 – Cheery 2014-10-01 17:53:04

1

的其他物質是指第二個如果,這樣的人會採取,如果$ facetype == 0

下一個問題是:你不檢查是否最後一個類型是之前的類型

我建議一個變量叫做$lasttype,並檢查是否$faceType等於說,如果沒有重置計數器,之後做輸出)

$flipCounts = 0; 
$matchingFaceTypes = 0; 
$ctype=0; 
$targetReached = false; 
$lasttype=-1; 

while ($matchingFaceTypes < 3) { 
    $faceType = rand(0, 1); 
    $flipCounts++; 
    if($faceType != $lasttype) { 
     if($lasttype!=-1) 
      echo " reset ". "\n<br/>"; 
     $lasttype=$faceType; 
     $matchingFaceTypes =0; 
    } 

    if($faceType == 0) { 
     $matchingFaceTypes++; 
     echo $faceType . " is heads ". "\n<br/>"; 
    }else{ 
     $matchingFaceTypes++; 
     echo $faceType." is tails ". "\n<br/>"; 
    }  
} 
echo "total flips took is: " . $flipCounts; 
+0

此作品謝謝。但我很困惑爲什麼最後一個類型等於-1。它只是一個隨機數? – Martynogea 2014-10-01 17:49:40

+0

-1是初始值,不會再出現,所以它不會在剛開始 – XoMEX 2014-10-01 17:55:19

+0

的時候說重置權,我用-5測試過了,代碼仍然有效。我也刪除了第二條if語句,仍然有效。我甚至自己創造了$ lasttype變量,並不等於任何東西。仍然有效。我實際上並沒有明白爲什麼它能夠正常工作。只要$ lasttype = $ facetType;在那裏,代碼工作。 – Martynogea 2014-10-01 18:17:23