我想計算金字塔回圈中的總數,我想分別計算所有奇數和偶數。我的代碼中有一個問題,奇數和偶數的計數不正確。
結果應該是:
總數:
奇數總數:
總偶數:
<?php
if (isset($_POST['btnsend'])) {
$oddCount = 0;
$evenCount = 0;
$num = $_POST['number'];
$width = strlen($_POST['number']) 1;
for ($row = 1; $row <= $num; $row ) {
if ($ % 2 == 0)
$evenCount ;
else
$oddCount ;
echo "<div style='display: flex; justify-content: center; text-align: center;'>";
for ($x = 1; $x <= $row; $x ) {
echo "<div style='width: {$width}ch; background: green;'>$row </div>";
}
echo "</div>";
}
echo "<p>You have {$evenCount} even and {$oddCount} odd numbers</p>";
}
uj5u.com熱心網友回復:
我認為你應該能夠自己解決這個問題。這不是一個難題。學習編程就是學習解決問題。如果你一直要求別人解決你的問題,你就不是在學習自己。
話雖如此,我認為您想要的是:
<?php
$userInput = 5;
$oddCount = 0;
$evenCount = 0;
$num = $userInput;
$width = strlen($num) 1;
for ($row = 1; $row <= $num; $row ) {
if ($row % 2 == 0) {
$evenCount = $row;
} else {
$oddCount = $row;
}
echo "<div style='display: flex; justify-content: center; text-align: center;'>";
for ($x = 1; $x <= $row; $x ) {
echo "<div style='width: {$width}ch; background: green;'>$row </div>";
}
echo "</div>";
}
echo "<p>You have {$evenCount} even and {$oddCount} odd numbers</p>";
我不得不洗掉,$_POST因為我沒有你的表格。對于5此回傳的輸入:
You have 6 even and 9 odd numbers
將輸入更改為10,您將獲得:
You have 30 even and 25 odd numbers
如您所見,這并不難。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446335.html
下一篇:如果值在物件陣列中包含字串 數字
