Ejercicios (sin formularios) - for (1) - Soluciones

En esta página se muestran una parte de las soluciones de los ejercicios for (1), concretamente el fragmento PHP, sin el principio ni el final de la página que se pueden dejar en fragmentos HTML.

Si lo prefiere, puede descargar unas posibles soluciones completas de estos ejercicios.

for (1) 01 - Círculos en fila

<?php
$circulos = rand(1, 10);

if ($circulos == 1) {
    print "  <h2>$circulos círculo</h2>\n";
} else {
    print "  <h2>$circulos círculos</h2>\n";
}
print "\n";
print "  <table class=\"conborde\">\n";
print "    <tbody>\n";
print "      <tr>\n";
for ($i = 0; $i < $circulos; $i++) {
    print "        <td>\n";
    print "          <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" width=\"70\" height=\"70\">\n";
    print "            <circle cx=\"35\" cy=\"35\" r=\"30\" fill=\"black\" />\n";
    print "          </svg>\n";
    print "        </td>\n";
}
print "      </tr>\n";
print "    </tbody>\n";
print "  </table>\n";
?>

for (1) 02 - Círculos en columna

<?php
$circulos = rand(1, 10);

if ($circulos == 1) {
    print "  <h2>$circulos círculo</h2>\n";
} else {
    print "  <h2>$circulos círculos</h2>\n";
}
print "\n";
print "  <table class=\"conborde\">\n";
print "    <tbody>\n";
for ($i = 0; $i < $circulos; $i++) {
    print "      <tr>\n";
    print "        <td>\n";
    print "          <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" width=\"70\" height=\"70\">\n";
    print "            <circle cx=\"35\" cy=\"35\" r=\"30\" fill=\"black\" />\n";
    print "          </svg>\n";
    print "        </td>\n";
    print "      </tr>\n";
}
print "    </tbody>\n";
print "  </table>\n";
?>

for (1) 03 - Círculos de colores

<?php
$circulos = rand(1, 10);

if ($circulos == 1) {
    print "  <h2>$circulos círculo</h2>\n";
} else {
    print "  <h2>$circulos círculos</h2>\n";
}
print "\n";
print "  <table class=\"conborde\">\n";
print "    <tbody>\n";
print "      <tr>\n";
for ($i = 0; $i < $circulos; $i++) {
    print "        <td>\n";
    print "          <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" width=\"70\" height=\"70\">\n";
    print "            <circle cx=\"35\" cy=\"35\" r=\"30\" fill=\"hwb(" . rand(1, 360) . " 0% 0%)\" />\n";
    print "          </svg>\n";
    print "        </td>\n";
}
print "      </tr>\n";
print "    </tbody>\n";
print "  </table>\n";
?>

for (1) 04 - Círculos numerados

<?php
$circulos = rand(1, 10);

if ($circulos == 1) {
    print "  <h2>$circulos círculo</h2>\n";
} else {
    print "  <h2>$circulos círculos</h2>\n";
}
print "\n";
print "  <table class=\"conborde\">\n";
print "    <tbody>\n";
print "      <tr>\n";
for ($i = 0; $i < $circulos; $i++) {
    print "        <td>\n";
    print "          <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" width=\"70\" height=\"70\" font-size=\"45\">\n";
    print "            <circle cx=\"35\" cy=\"35\" r=\"30\" fill=\"hwb(" . rand(1, 360) . " 0% 0%)\" />\n";
    print "            <text x=\"35\" y=\"50\" text-anchor=\"middle\">" . rand(1, 9) . "</text>\n";
    print "          </svg>\n";
    print "        </td>\n";
}
print "      </tr>\n";
print "    </tbody>\n";
print "  </table>\n";
?>

for (1) 05 - Círculos girados

<?php
$circulos = rand(1, 10);

if ($circulos == 1) {
    print "  <h2>$circulos círculo</h2>\n";
} else {
    print "  <h2>$circulos círculos</h2>\n";
}
print "\n";
print "  <table class=\"conborde\">\n";
print "    <tbody>\n";
print "      <tr>\n";
for ($i = 0; $i < $circulos; $i++) {
    print "        <td>\n";
    print "          <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" width=\"70\" height=\"70\" font-size=\"45\">\n";
    print "            <circle cx=\"35\" cy=\"35\" r=\"30\" fill=\"hwb(" . rand(1, 360) . " 0% 0%)\" />\n";
    print "            <text x=\"35\" y=\"50\" text-anchor=\"middle\" transform=\"rotate(" . rand(-80, 80) . " 35 35)\">" . rand(1, 9) . "</text>\n";
    print "          </svg>\n";
    print "        </td>\n";
}
print "      </tr>\n";
print "    </tbody>\n";
print "  </table>\n";
?>

for (1) 11 - Contar puntos

<?php
$numero = rand(1, 10);
$total = 0;

if ($numero == 1) {
    print "  <h2>$numero dado</h2>\n";
} else {
    print "  <h2>$numero dados</h2>\n";
}
print "\n";
print "  <p>\n";
for ($i = 0; $i < $numero; $i++) {
    $dado = rand(1, 6);
    print "    <img src=\"img/$dado.svg\" alt=\"$dado\" width=\"140\" height=\"140\">\n";
    $total += $dado;
}
print "  </p>\n";
print "\n";
print "  <p>El total de puntos obtenidos es <strong>$total</strong>.</p>\n";
?>

for (1) 12 - Contar pares e impares

<?php
$numero = rand(1, 10);
$pares = 0;
$impares = 0;

if ($numero == 1) {
    print "  <h2>$numero dado</h2>\n";
} else {
    print "  <h2>$numero dados</h2>\n";
}
print "\n";
print "  <p>\n";
for ($i = 0; $i < $numero; $i++) {
    $dado = rand(1, 6);
    print "    <img src=\"img/$dado.svg\" alt=\"$dado\" width=\"140\" height=\"140\">\n";
    if ($dado % 2) {
        $impares += 1;
    } else {
        $pares += 1;
    }
}
print "  </p>\n";
print "\n";
print "  <p>Han salido ";
if ($pares == 1) {
    print "1 número par y ";
} else {
    print "$pares números pares y ";
}
if ($impares == 1) {
    print "1 número impar.</p>\n";
} else {
    print "$impares números impares.</p>\n";
}
?>

for (1) 13 - Dado máximo

<?php
$numero = rand(1, 10);
$maximo = 0;

if ($numero == 1) {
    print "  <h2>$numero dado</h2>\n";
} else {
    print "  <h2>$numero dados</h2>\n";
}
print "\n";
print "  <p>\n";
for ($i = 0; $i < $numero; $i++) {
    $dado = rand(1, 6);
    print "    <img src=\"img/$dado.svg\" alt=\"$dado\" width=\"140\" height=\"140\">\n";
    if ($dado > $maximo) {
        $maximo = $dado;
    }
}
print "  </p>\n";
print "\n";
print "  <p>El valor más grande obtenido es <strong>$maximo</strong>.</p>\n";
?>

for (1) 14 - Dado mínimo

<?php
$numero = rand(1, 10);
$minimo = 7;

if ($numero == 1) {
    print "  <h2>$numero dado</h2>\n";
} else {
    print "  <h2>$numero dados</h2>\n";
}
print "\n";
print "  <p>\n";
for ($i = 0; $i < $numero; $i++) {
    $dado = rand(1, 6);
    print "    <img src=\"img/$dado.svg\" alt=\"$dado\" width=\"140\" height=\"140\">\n";
    if ($dado < $minimo) {
        $minimo = $dado;
    }
}
print "  </p>\n";
print "\n";
print "  <p>El valor más pequeño obtenido es <strong>$minimo</strong>.</p>\n";
?>

for (1) 15 - Contar dados máximos

<?php
$numero = rand(1, 10);
$maximo = 0;
$cantidad = 0;

if ($numero == 1) {
    print "  <h2>$numero dado</h2>\n";
} else {
    print "  <h2>$numero dados</h2>\n";
}
print "\n";
print "  <p>\n";
for ($i = 0; $i < $numero; $i++) {
    $dado = rand(1, 6);
    print "    <img src=\"img/$dado.svg\" alt=\"$dado\" width=\"140\" height=\"140\">\n";
    if ($dado > $maximo) {
        $maximo = $dado;
        $cantidad = 1;
    }
    elseif ($dado == $maximo) {
        $cantidad += 1;
    }
}
print "  </p>\n";
print "\n";
print "  <p>El valor más grande obtenido es <strong>$maximo</strong>. Este valor se ha obtenido ";
if ($cantidad == 1) {
    print "<strong>$cantidad</strong> vez.</p>\n";
} else {
    print "<strong>$cantidad</strong> veces.</p>\n";
}
?>

for (1) 21 - Diana (1)

<?php
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n";
print "      width=\"420\" height=\"420\" viewBox=\"-210 -210 420 420\">\n";

for ($i = 0; $i < 5; $i++) {
    print "      <circle cx=\"0\" cy=\"0\" r=\"" . 200 - 40 * $i . "\" fill=\"red\" />\n";
    print "      <circle cx=\"0\" cy=\"0\" r=\"" . 200 - 40 * $i - 20 . "\" fill=\"#ddd\" />\n";
}

for ($i = 0; $i < 10; $i++) {
    print "      <text x=\"0\" y=\"" . 195 - 20 * $i . "\" text-anchor=\"middle\" font-size=\"13\">" . $i + 1 . "</text>\n";
}

$puntos = 0;
$disparos = rand(1, 10);
for ($i = 0; $i < $disparos; $i++) {
    $x = rand(-200, 200);
    $y = rand(-200, 200);
    $puntos += ((10 - intdiv(sqrt($x ** 2 + $y ** 2), 20)) > 0 ? (10 - intdiv(sqrt($x ** 2 + $y ** 2), 20)) : 0);
    print "      <path fill=\"black\" stroke=\"white\" stroke-width=\"2\" "
     . "d=\"m $x,$y m -1,-9 4.4,2.5 3.6,0.6 0.5,2.9 2.2,2.9 -2.2,3.1 "
     . "-0.1,3.6 -3.3,0.2 -1.7,2.7 -4,-1.4 -3.9,0.2 -0.9,-4.2 -2.7,-2.6 1.7,-3.4 0,-3 z\" />\n";
}
print "    </svg>\n";
print "  </p>\n";
print "\n";
print "  <h2>Estadísticas</h2>\n";
print "\n";
print "  <ul>\n";
print "    <li>Número de disparos: <strong>$disparos</strong>.</li>\n";
print "    <li>Puntuación obtenida: <strong>$puntos</strong>.</li>\n";
print "  </ul>\n";
?>