-3

此脚本的目的:

编写一个以 1 为步从 1 到 10 的脚本。对于每个数字,显示该数字是奇数还是偶数,如果该数字是质数,还显示一条消息。在 HTML 表格中显示此信息。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Exercise 1</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
  th { text-align: left; background-color: #999; }
  th, td { padding: 0.4em; }
  tr.alt td { background: #ddd; }
</style>
</head>
<body>

<h2>Exercise 1</h2>

<table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;">
  <tr>
    <th>Number</th>
    <th>Parity</th>
    <th>Primality</th>
  </tr>
    <?php 
    $n=10;
    for ($i=1;$i<=$n;$i++){
    echo ($i%2 != 0)? '<tr class = "alt">':'<tr>'; ?>
    <td><?php echo $i; ?></td>
    <td><?php echo ($i%2 != 0)? "Odd":"Even";?></td>
    <td><?php 

    $k=0;
    for ($j=1;$j<=$i;$j++){
        if ($i%$j=0) $k++; //Where the error occurs
    }
    echo ($k=2 || $k=1)?"Prime":"Composite";?>
    </td></tr>
    <?php
    }
    ?>
</table>
</body>
</html>
4

2 回答 2

0

if ($i%$j=0)应该是if ($i%$j==0)

于 2013-03-02T16:48:31.690 回答
0

$i%$j=0

赋值$j0然后尝试做$i % 0

你要

($i % $j) == 0

以同样的方式还有另一个错误echo

还可以考虑使用空格格式化代码以使其更具可读性。

于 2013-03-02T16:48:40.303 回答