数字柜台游戏 [英] Number counter game

查看:53
本文介绍了数字柜台游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个使用for循环打印6个随机数1-6的随机数计数器游戏。我想这样做,以便代码可以说出数字6在循环中显示了多少次。



目前,我有打印出来的代码,可以循环显示6个随机数,但它只计算打印出的数字。



例如
欢迎来到骰子游戏!
您将滚动多少个六度?
4 2 4 6 4 6
您掷了2个6个!

 <?php 

echo< h1>欢迎来到猜猜游戏事物猜猜多少6s!< / h1>;
$ counter = 0;

for($ i = 0; $ i< = 6; $ i ++){
$ randomNum = rand(1,6);

if($ randomNum< = 6){
echo< br> $ randomNum;
$ counter ++;
}

else
{
echo $ randomNum< br>;

}
}

echo< br>您滚动了$ counter sixes;


解决方案

您可以这样做:

  $ num = $ _POST [ num]; 

for($ i = 0; $ i< = 100; $ i ++){
$ randomNum = rand(1,10);

if($ randomNum == $ num){
echo $ randomNum;
休息时间;
}

echo $ randomNum;

}

echo< h2>有$ i< / h2>;


Im trying to make a random number counter game that uses a for loop to print out 6 random numbers 1 - 6. I want to make it so the code can say how many times the number 6 shows in the loop.

At the moment I have the code it prints out for a loop of 6 random numbers but it only counts the numbers printed out.

For example Welcome to the Dice Game! How many sixes will you roll? 4 2 4 6 4 6 You rolled 2 six(es)!

<?php

echo"<h1>Welcome to the guess game thing guess how many 6s!</h1>";
$counter = 0;

for ($i=0; $i <=6;$i++) { 
    $randomNum = rand(1,6);

    if ($randomNum <= 6) {
        echo "<br> $randomNum";
        $counter++;
    }

    else
    {
        echo"$randomNum  <br>";

    }
}

echo"<br>You rolled $counter sixes";

解决方案

You can do it like this:

$num = $_POST["num"];

for ($i=0; $i <=100;$i++) { 
    $randomNum = rand(1,10);

    if ($randomNum == $num) {
        echo $randomNum;
        break;
    }

    echo $randomNum;

}

echo"<h2> there are $i</h2>";

这篇关于数字柜台游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆