1
0

More than 3 years have passed since last update.

php】phpでコイントス問題を解け

Posted at

1・変数 $count で指定した回数だけコイントスを行い、「コイン投げの回数: [$countの値] 回」を出力
2・表が出たら「H」(表を表す Head の略です。)、裏が出たら「T」(裏を表す Tail の略です。)を出力する
3・Hの文字は赤色、Tの文字は青色で出力する
4・コイントス終了後に表が出た回数を裏が出た回数を出力する
5・乱数を取得する関数mt_randを利用する。
6.表が出たらH、裏が出たらTを配列に追加し、またそれぞれのカウントを追加。
7・出力時には「[H or T]」の形式で出力せよ。

image.png


<!DOCTYPE html>
<html lang="ja">
<head>
    <title>コイントス</title>
    <!-- レイアウト画像の通り、表に枠線を追加する -->
    <style>
      .coin_h {
        color: #FF0000;
      }
      .coin_t {
        color: #0000FF
      }
    </style>
</head>
<body>
  <h1>コイントス</h1>
<?php
$i = 0;
$h = 0;
$t = 0;
$count = 100;
echo "コイン投げの回数:".$count ."回". '<br>';


while ($i < $count){

  $rand = mt_rand(1, 2);

  if($rand % 2 === 0){
   ?> 
  <span class="coin_h">H</span>   <?php 
    $h++;

  }else{ ?>
    <span class="coin_t">T</span>   <?php 
      $t++;

  }

  $i++;
}
echo "<br>";
echo "表が出た回数:".$h ."回". '<br>';
echo "裏が出た回数:".$t ."回". '<br>';

?>

</body>
</html>

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0