2015-06-17 93 views
0

我很新的編程,我想創建一個轉換器從攝氏度到華氏溫度到開爾文。用戶輸入他們想要在2個輸入框中轉換的值(以攝氏度爲單位),並使用循環創建一個表格。第一組數據以攝氏度輸出,但第二組數據流(華氏溫度)僅輸出一個攝氏溫度值,即攝氏溫度環中最後一個節點的轉換值。PHP(簡單):麻煩與循環

<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

您的代碼有2個問題。

一個問題是,你只是分配一次c,當你第一次運行的時候,它正在計數C,然後當你到達第二個do {} while()時,它已經是最大值了。

應後的第一個「復位」 C while循環喜歡這裏:

<th>Degrees Fahrenheit</th> 

       <?php 
    $c = $one - 1; 

其次你只計算你的˚F變量一次,您應該做它的功能(可能在這種情況下是矯枉過正)或移動您的while循環中˚F下來計算,你的代碼的最後一部分會是這樣的 華氏度

  <?php 
       $c = $one - 1; 
          do { 
           $f = (1.8 * $c) + 32; 
           $c++; 
           echo "<td>" . $f . "</td>"; 
          } while ($c < $two); 

          $k = $x - 273; 
      ?> 
     </tr>