Php 如何连续找到匹配的硬币?

Php 如何连续找到匹配的硬币?,php,loops,if-statement,while-loop,Php,Loops,If Statement,While Loop,我想找到一排匹配的硬币面。我设法做到了这一点,使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; 而($matchingFaceTypes0,'to

我想找到一排匹配的硬币面。我设法做到了这一点,使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;
而($matchingFaceTypes<3){
$faceType=rand(0,1);
$flipCounts++;
如果($faceType==0){
$matchingFaceTypes++;
echo$faceType。“是头”。\n;
}
如果($faceType==1){
$matchingFaceTypes++;
echo$faceType。“是尾部”。\n;
}否则{
$matchingFaceTypes=0;
回显“重置”。\n;
}
}echo“总翻转次数为:”$翻转计数;

else指的是第二个if,因此如果$facetype==0,则采用else

下一个问题:您没有检查最后一个类型是否与前一个相同

我建议使用一个名为
$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;
$flipCounts=0;
$matchingFaceTypes=0;
$ctype=0;
$targetReached=false;
$lasttype=-1;
而($matchingFaceTypes<3){
$faceType=rand(0,1);
$flipCounts++;
如果($faceType!=$lasttype){
如果($lasttype!=-1)
回显“重置”。\n
; $lasttype=$faceType; $matchingFaceTypes=0; } 如果($faceType==0){ $matchingFaceTypes++; echo$faceType。“是头”。\n
; }否则{ $matchingFaceTypes++; echo$faceType。“是尾部”。\n
; } } echo“总翻转次数为:”$翻转计数;
$maxMatches=3;
$matches=array('tails'=>0,'heads'=>0,'total'=>0);
而(最大($matches['tails',$matches['heads])<$maxMatches){
$faceType=rand(0,1);
如果($faceType){
$matches['heads']++;
$matches['tails']=0;
echo$faceType.“是头\n”;
}
否则{
$matches['tails']++;
$matches['heads']=0;
echo$faceType.“是尾部\n”;
}
$matches['total']++;
}
echo“总翻转次数为:”$匹配['total'];
说到马克斯

$maxMatches=3;
$total=0;
$matches=数组('tails'=>0,'heads'=>0);
而(最大($matches)<$maxMatches){
$faceType=rand(0,1);
如果($faceType){
$matches['heads']++;
$matches['tails']=0;
echo$faceType.“是头\n”;
}
否则{
$matches['tails']++;
$matches['heads']=0;
echo$faceType.“是尾部\n”;
}
$total++;
}
echo“总翻转次数为:”$全部的

您正在设置
$matchingFaceTypes=0
任何时间
$faceType==1
为false。您要做的是跟踪上一个类型是什么,如果当前类型与上一个类型不相同,请重置。谢谢。我想问,是否需要在while参数中使用max?@Martynogea您可以分别检查每个元素
heads
tails
。我的路稍微短一点。如果你将
total
移动到一个单独的变量,那么你可以只做
max($matches)<$maxMatches
@Martynogea我写了另一个例子,其中
max()
是有用的。我认为第二个带有附加变量的例子更容易理解。附言:刚刚计算了字符数,第二个总共有437个字符,而你说的较短的另一个有489个字符。但是我理解你是从哪里来的,你说它比较短。@Martynogea shorter并不意味着“更少的字符数”。我可以使用一个字母的变量,将所有内容都写在一行中,等等,使其“更短”。我的意思是更容易阅读和理解代码。谢谢。但我不明白为什么最后一个类型等于-1。这仅仅是一个随机数吗,-1是初始值,永远不会再出现,所以它不会在一开始就说重置。我用-5测试,代码仍然有效。我还删除了第二个if语句,仍然有效。我甚至自己创建了$lasttype变量,它不等于任何东西。仍然有效。我不明白为什么它工作得很好。只要$lasttype=$facetType;你也可以把这个值转换成任何值(甚至是空值或类似的值),只要你在两个地方都改变它(定义和检查)。。。如果你不在两个地方都这样做,当然它会工作,但是第一个输出会是“重置”,现在我说在第一次滚动之前,lasttype是-1,在所有滚动中,我检查这个变量是否仍然是-1,因为我知道这是第一次滚动,因此我抑制了第一次“重置”
$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;
$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;