2015-04-27 50 views
0

我遇到了我的代碼問題。華氏溫度和攝氏溫度的轉換不會顯示,當我選擇「攝氏溫度到華氏度」時,它會使我將華氏轉換爲攝氏溫度。我不確定我做錯了什麼。我已經尋求幫助,並沒有得到任何幫助。所以任何幫助將不勝感激!這裏是如果需要這種耐心的鏈接:(修訂)溫度轉換器Perl

#!/usr/bin/perl 
#c07case1.cgi - Temperature conversion 
print "Content-type: text/html\n\n"; 
use CGI qw(:standard); 
use strict; 

#variables 
my ($temp, $fahConvert, $celConvert); 
$temp = param('temp'); 
$unit = param('unit'); 
$fahConvert = param('fah'); 
$celConvert = param('cel'); 

#calculate 
    if ($unit eq 'cel') { 
     cel(); 
     $celConvert = ($temp - 32) * 5/9; 

    } 
    else { 
     fah(); 
     $fahConvert = $temp * 9/5 + 32; 
    } 
sub cel { 
    print "<html>\n"; 
    print "<head><title>Conversion from Celsius</title></head>\n"; 
    print "<body>\n"; 
    print "<h3>Celsius: $temp </h3>\n"; 
    print "<h3>Fahrenheit: $fahConvert </h3>\n"; 
    print "</body>\n"; 
    print "</html>\n"; 
} 
sub fah { 
    print "<html>\n"; 
    print "<head><title>Conversion from Fahrenheit</title></head>\n"; 
    print "<body>\n"; 
    print "<h3>Fahrenheit: $temp </h3>\n"; 
    print "<h3>Celsius: $celConvert </h3>\n"; 
    print "</body>\n"; 
    print "</html>\n"; 
} 

HTML腳本:https://crux.baker.edu/~wmorni01/WEB221_HTML/chapxx/c07case1.html

Perl腳本(修訂)

<!c07case1.html> 
<HTML> 
<HEAD><TITLE>Washington Middle School</TITLE></HEAD> 
<BODY> 
<H1>Temperature Converter</H1> 
<HR> 
<FORM ACTION="https://crux.baker.edu/~wmorni01/cgi-bin/chap07_wendy_morningstar/c07case1.cgi" METHOD=POST> 
<P><B>Temperature:</B> <INPUT TYPE=text NAME=temp></P> 

<INPUT TYPE=radio NAME=unit Value=fah CHECKED>Fahrenheit to Celsius<BR> 
<INPUT TYPE=radio NAME=unit Value=cel>Celsius to Fahrenheit<BR> 

<P><INPUT TYPE=submit VALUE=Convert></P> 
</FORM></BODY></HTML> 

回答

1

您輸入這兩個被命名爲temp。從解決這個問題開始。

<P><B>Temperature:</B> <INPUT TYPE=text NAME=temp></P> 

<INPUT TYPE=radio NAME=unit Value=fah CHECKED>Fahrenheit to Celsius<BR> 
<INPUT TYPE=radio NAME=unit Value=cel>Celsius to Fahrenheit<BR> 

入門的兩個字段的值是使用以下進行:

my $temp = param('temp'); # Grab the temperature 
my $unit = param('unit'); # "fah" or "cel" 

檢查,如果溫度在攝氏使用以下完成:

if ($unit eq 'cel') { ... } 

最後,僅在之後計算轉換後的值,生成使用將用於存儲該值的變量的HTML。您可以簡單地顛倒計算和子調用的順序,但如果您避免使用全局變量而非參數,那將是最好的選擇。

#!/usr/bin/perl 
#c07case1.cgi - Temperature conversion 

use strict; 
use warnings; 

use CGI qw(:standard); 

sub fah { 
    my ($cel, $fah) = @_; 
    print "<html>\n"; 
    print "<head><title>Conversion to Fahrenheit</title></head>\n"; 
    print "<body>\n"; 
    print "<h3>Celsius: $cel </h3>\n"; 
    print "<h3>Fahrenheit: $fah </h3>\n"; 
    print "</body>\n"; 
    print "</html>\n"; 
} 

sub cel { 
    my ($fah, $cel) = @_; 
    print "<html>\n"; 
    print "<head><title>Conversion to Celcius</title></head>\n"; 
    print "<body>\n"; 
    print "<h3>Fahrenheit: $fah </h3>\n"; 
    print "<h3>Celsius: $cel </h3>\n"; 
    print "</body>\n"; 
    print "</html>\n"; 
} 

{ 
    print "Content-type: text/html\n\n"; 

    my $temp = param('temp'); # Grab the temperature 
    my $unit = param('unit'); # "fah" or "cel" 

    if ($unit eq 'cel') { 
     cel($temp, ($temp - 32) * 5/9); 
    } else { 
     fah($temp, $temp * 9/5 + 32); 
    } 
} 
+0

好吧,它給我的攝氏度/華氏度,反之亦然正確,但它仍然不顯示轉換。 –

+0

我不知道爲什麼這是downvoted ... –

+0

謝謝你的幫助!我相信它現在可以正常工作! –