如何在桌子内做钻石图案/形状(星号)? (html + php)

时间:2016-11-25 12:19:01

标签: php html html-table

我必须在桌子内使用for循环制作菱形星号。 必须有&#34;空白&#34;在星号前后移动<td>个空格并使其看起来居中,因此它看起来像钻石。我怎么做? (我在HTML代码中使用了PHP。)

没有<tr><td>标签的代码,它看起来像钻石,因为它是居中对齐的:

<center>
<?php
echo "<table border = 1>";

    // loop for the pyramid

        for($i = 1; $i <= 10; $i += 2) {
            for($j = 1; $j <= $i; $j++) {
                echo "* ";
            }
        echo "<br />";
        }

    // loop for the inverted pyramid, so it looks like a diamond

    for($i = 7; $i >= 1; $i -= 2) {
        for($j = 1; $j <= $i; $j++) {
            echo "* ";
        }   
    echo "<br />";
    }

echo "</table>";
?>
</center>

包含<tr><td>代码的代码,需要&#34;空格&#34;因为它看起来像是中心对齐的:

<?php
echo "<table border = 1>";

    // loop for the pyramid

    echo "<tr>";
            for($i = 1; $i <= 10; $i += 2) {
            echo "<tr>";
                for($j = 1; $j <= $i; $j++) {
                    echo "<td>* </td>";
                }
            echo "</tr>";
        }
    echo "</tr>";

    // loop for the inverted pyramid, so it looks like a diamond

    for($i = 7; $i >= 1; $i -= 2) {
    echo "<tr>";
        for($j = 1; $j <= $i; $j++) {
            echo "<td>* </td>";
        }   
    echo "<br />";
    echo "</tr>";
    }

echo "</table>";
?>

请帮忙!

4 个答案:

答案 0 :(得分:4)

这是您的解决方案的新代码。我添加了逻辑,将空白td向前和向后放到*

$connectDb = $services->get('connectDb');
$connectOAuth2 = $services->get('connectoauth2'); // --> service is not found
$service = new \ConnectApp\Service\UserEducation($connectDb, $connectOAuth2);
return new UserEducationResource($service);

答案 1 :(得分:1)

我使用下面的代码而不使用表格来制作菱形。

<div style="text-align: center">
<?php
$n = 8;

if($n === 1){ die("input must be greater than 1"); }

$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;

for($x = 1; $x <= $nn; $x++){
    $temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
    $total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);

    echo nl2br(str_repeat('* &nbsp;', $total) . "\r\n");
}
?>

答案 2 :(得分:0)

我使用了下面的代码。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/env/lib/python3.5/site-packages/dask/dataframe/utils.py in raise_on_meta_error(funcname, udf)
    159     try:
--> 160         yield
    161     except Exception as e:

~/env/lib/python3.5/site-packages/dask/dataframe/core.py in elemwise(op, *args, **kwargs)
   3426         with raise_on_meta_error(funcname(op)):
-> 3427             meta = partial_by_order(*parts, function=op, other=other)
   3428 

~/env/lib/python3.5/site-packages/dask/utils.py in partial_by_order(*args, **kwargs)
    903         args2.insert(i, arg)
--> 904     return function(*args2, **kwargs)
    905 

~/env/lib/python3.5/site-packages/pandas/core/ops.py in f(self, other)
   2090 
-> 2091         other = _align_method_FRAME(self, other, axis=None)
   2092 

~/env/lib/python3.5/site-packages/pandas/core/ops.py in _align_method_FRAME(left, right, axis)
   1984         # GH17901
-> 1985         right = to_series(right)
   1986 

~/env/lib/python3.5/site-packages/pandas/core/ops.py in to_series(right)
   1946                 raise ValueError(msg.format(req_len=len(left.columns),
-> 1947                                             given_len=len(right)))
   1948             right = left._constructor_sliced(right, index=left.columns)

ValueError: Unable to coerce to Series, length must be 3: given 4

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-5-ce21a4e5e37e> in <module>
      1 my_var = [2,'wew','ewwew','44']
----> 2 [ k for k,v in locals().items() if v == my_var][0]

<ipython-input-5-ce21a4e5e37e> in <listcomp>(.0)
      1 my_var = [2,'wew','ewwew','44']
----> 2 [ k for k,v in locals().items() if v == my_var][0]

~/env/lib/python3.5/site-packages/dask/dataframe/core.py in <lambda>(self, other)
   1199             return lambda self, other: elemwise(op, other, self)
   1200         else:
-> 1201             return lambda self, other: elemwise(op, self, other)
   1202 
   1203     def rolling(self, window, min_periods=None, freq=None, center=False,

~/env/lib/python3.5/site-packages/dask/dataframe/core.py in elemwise(op, *args, **kwargs)
   3425                  else d._meta_nonempty for d in dasks]
   3426         with raise_on_meta_error(funcname(op)):
-> 3427             meta = partial_by_order(*parts, function=op, other=other)
   3428 
   3429     result = new_dd_object(graph, _name, meta, divisions)

/usr/lib/python3.5/contextlib.py in __exit__(self, type, value, traceback)
     75                 value = type()
     76             try:
---> 77                 self.gen.throw(type, value, traceback)
     78                 raise RuntimeError("generator didn't stop after throw()")
     79             except StopIteration as exc:

~/env/lib/python3.5/site-packages/dask/dataframe/utils.py in raise_on_meta_error(funcname, udf)
    175                 "{2}")
    176         msg = msg.format(" in `{0}`".format(funcname) if funcname else "", repr(e), tb)
--> 177         raise ValueError(msg)
    178 
    179 

ValueError: Metadata inference failed in `eq`.

Original error is below:
------------------------
ValueError('Unable to coerce to Series, length must be 3: given 4',)

Traceback:
---------
  File "/home/michael/env/lib/python3.5/site-packages/dask/dataframe/utils.py", line 160, in raise_on_meta_error
    yield
  File "/home/michael/env/lib/python3.5/site-packages/dask/dataframe/core.py", line 3427, in elemwise
    meta = partial_by_order(*parts, function=op, other=other)
  File "/home/michael/env/lib/python3.5/site-packages/dask/utils.py", line 904, in partial_by_order
    return function(*args2, **kwargs)
  File "/home/michael/env/lib/python3.5/site-packages/pandas/core/ops.py", line 2091, in f
    other = _align_method_FRAME(self, other, axis=None)
  File "/home/michael/env/lib/python3.5/site-packages/pandas/core/ops.py", line 1985, in _align_method_FRAME
    right = to_series(right)
  File "/home/michael/env/lib/python3.5/site-packages/pandas/core/ops.py", line 1947, in to_series
    given_len=len(right)))

答案 3 :(得分:-1)

<?php
    for($i=0;$i<=5;$i++){
        for($j=5;$j>=$i;$j--){
            echo '&nbsp;';
        }
        for($k=0;$k<=$i;$k++){
            echo '*';
        }
        echo '<br />';
    }
    for($i=0;$i<=4;$i++){
        for($k=0;$k<=$i+1;$k++){
            echo '&nbsp;';
        }
        for($j=4;$j>=$i;$j--){
            echo '*';
        }
        echo '<br />';
    }
?>