duanke8011 2015-06-17 18:41
浏览 17
已采纳

PHP(简单):循环问题

I'm very new to programming, I am trying to take create a converter from Celsius to Fahrenheit to Kelvin. The user inputs the values (in celsius) they want converted in the 2 input boxes and it creates a table using loops. The first set of data where it outputs the amounts in celsius looks great however the second data stream (fahrenheit) will only output one value of celsius which is the converted value of the final nuber in the celsius loop.

<form name="calculator" action="" method="post">
    From: <input class="inputbox" type="number" name="one" value="" /><br />
                <p>to</p><br>
    To: <input class="inputbox" type="number" name="two" value="" /><br />
        <input type="submit" class="submit" name="submit" value="Get Conversions!" />
    </form>
    <br>

    <table border='1' cellpadding='5px'>
            <tr>
                <th>Degrees Celsius</th>

                <?php 
                    if ($_POST['submit']) {
                        $one = $_POST['one'];
                        $two = $_POST['two'];
                    }

                        if ($two < $one) {
                            echo "<p>Please put the lowest number in the first input box.</p>";
                        } else if ($one < (-273) OR $two < (-273)) {
                            echo "<p>   Tempature cant go below -273 Celsius (0 kelvin), please enter higher values.</p>";
                        } else {
                            $c = $one - 1;

                                do {
                                    $c++;
                                    echo "<td>" . $c . "</td>";
                                } while ($c < $two);
                        }

                ?>
            </tr>
            <tr>
                <th>Degrees Fahrenheit</th>

                <?php 

                    $f = (1.8 * $c) + 32;

                                do {
                                    $c++;
                                    echo "<td>" . $f . "</td>";
                                } while ($c < $two);

                                $k = $x - 273;
                ?>
            </tr>
    </table>
  • 写回答

1条回答 默认 最新

  • dongshang5862 2015-06-17 18:48
    关注

    Your have 2 problems in your code.

    One problem is that you are only assigning c once, when you are running the first while, it's counting C up and then when you get to the second do{}while() it's allready at the maximum.

    you should "reset" C after The first while loop like here:

    <th>Degrees Fahrenheit</th>
    
                    <?php 
      $c = $one - 1;
    

    Secondly your only counting your f variable once, you should either make a function for it (might be overkill in this case) or move your calculation of f down inside the while loop, the last part of your code would be something like this Degrees Fahrenheit

                <?php 
                    $c = $one - 1;
                                do {
                                    $f = (1.8 * $c) + 32;
                                    $c++;
                                    echo "<td>" . $f . "</td>";
                                } while ($c < $two);
    
                                $k = $x - 273;
                ?>
            </tr>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿