dongyue7796 2013-03-02 16:44
浏览 84
已采纳

嵌套for循环中的“除以零”错误

The purpose of this script:

Write a script that counts from 1 to 10 in steps of 1. For each number, display whether that number is an odd or even number, and also display a message if the number is a prime number. Display this information within an HTML table.

<!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>
  • 写回答

2条回答 默认 最新

  • douduoquan2824 2013-03-02 16:48
    关注

    $i%$j=0

    Assigns $j as 0 and then tries to do $i % 0;

    You want

    ($i % $j) == 0

    There's another mistake in the same way with the echo.

    Consider also formatting your code with spaces to make it more readable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了