【问题标题】:How to check prime number with an input set as LIMIT如何检查输入设置为 LIMIT 的素数
【发布时间】:2018-02-17 05:41:39
【问题描述】:

所以我有代码来检查带有输入值的素数。我的问题是,我怎样才能转动我的代码,以便表格可以以 10 个为一组动态,并且最大值为 100?这样输入只能以 10 为增量,而您可以输入的最大值为 100?

我当前的代码现在设置为 10 列和 4 行,所以有没有办法进行这种更改(我猜是动态的?)以 10 的输入增量进行更改?

这是我的代码:

<form method="post" action="">
     <table border="2" width="1180px">
        <thead>
            <center>
                Number Chart</center>

        </thead>

    <?php
    error_reporting(0);

    function isPrime($n)
    {
    if ($n == 1) return false;
    if ($n == 2) return true; 
    if ($n % 2 == 0)
        {
        return false;
        }

    $i = 2;
    for ($i = 2; $i < $n; $i++)
        {
        if ($n % $i == 0)
            {
            return false;
            }
        }

    return true;
    }


    if (isset($_POST['value']) && $_POST['value'] != 0)
    {
    /* @var $start type */
    $start = $_POST['value'];
    }
    else
    {
    $start = 1;
    }


    $n_cols = 10;
    $n_rows = 5;

    for ($i = 0; $i < $n_rows; $i++) // 
    {
    $col = '';
    for ($j = 0; $j < $n_cols; $j++)
        {
        $number = ($start - $i - ($j * $n_cols));

        if (isPrime($number) == true)
            {
            //if prime color it red
            $col.= '<th style="color:red">' . ($start - $j - ($i * $n_cols)) . 
    '</th>';
            }
          else
            {
            $col.= '<th>' . ($start - $j - ($i * $n_cols)) . '</th>';
            }
        }

    $out.= '<tr>' . $col . $row . '</tr>';
    }

    echo $out;
    ?>

     <tr>   
            <thead colspan=10>
                    <center>
                        <label for="input">Enter Limit:</label>
                        <input type="text" name="value" style="width: 60px">
                        <input type="submit" name="submit" value="Submit">
                    </center>
            </thead>
        </tr>
    </table>
    </form>

【问题讨论】:

  • 重新开启错误报告
  • 完成了,现在怎么办?
  • 我会将输入更改为选择并提供 10,20...100 的选项,如果我理解正确,应该可以解决它
  • @rtfm 那么作为单选按钮?分配是在输入框中输入一个值,但是如果您不输入任何内容,除了增量 10 会给出错误提示“您必须输入有效值”
  • 没有选择&lt;select name='value&gt;&lt;option value="10"&gt;10&lt;/option&gt;&lt;option value="20"&gt;20&lt;/option&gt; etc &lt;/select&gt;

标签: php forms html-table primes


【解决方案1】:
if (($_POST['value'] % 10) != 0 || $_POST['value'] >100 ){ 

echo 'you must enter a valid value';

}else{
  //all the code you want to run only if the number posted is valid
}

% 是取模,详情请参阅http://php.net/manual/en/language.operators.arithmetic.php

【讨论】:

  • 伙计,这太棒了,它起作用了.....现在......我需要的最后一件事是表格可以更改为输入吗?所以列总是10。但它可以根据输入改变行吗?例如,如果它是 10,它只有一行,因为它有 10 列。如果我输入 20,那么 2 行、30、3 行,依此类推......
  • 基础数学 10/10=1、20/10=2 等
  • 那么我将如何编写以便行取决于用户输入的输入?我会在定义行的地方使用 % 吗?
  • 我真的不明白你怎么能做到这一点,基于你到目前为止所做的。
  • 谢谢我想通了...我应该用所有正确的编码编辑我的帖子并将其标记为已解决吗?
猜你喜欢
  • 1970-01-01
  • 2014-04-22
  • 2015-12-02
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 2021-03-30
  • 2021-11-15
  • 2023-01-17
相关资源
最近更新 更多