Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl华氏温度到摄氏温度的转换,反之亦然_Perl - Fatal编程技术网

Perl华氏温度到摄氏温度的转换,反之亦然

Perl华氏温度到摄氏温度的转换,反之亦然,perl,Perl,我有一个Perl问题: 编写一个Perl脚本,询问用户温度,然后询问温度是转换为摄氏度还是华氏度。执行转换并显示答案。温度转换的方程式为: 1) Celsius to Fahrenheit:C=(F-32) x 5/9 2) Fahrenheit to Celsius:F=9C/5 + 32 我的剧本是: #!/usr/bin/perl use strict; use warnings; print "Enter the temperature: "; my $temp = <STDI

我有一个Perl问题: 编写一个Perl脚本,询问用户温度,然后询问温度是转换为摄氏度还是华氏度。执行转换并显示答案。温度转换的方程式为:

1) Celsius to Fahrenheit:C=(F-32) x 5/9
2) Fahrenheit to Celsius:F=9C/5 + 32
我的剧本是:

#!/usr/bin/perl
use strict;
use warnings;

print "Enter the temperature: ";
my $temp = <STDIN>;
print "Enter the Conversion to be performed:";
my $conv = <STDIN>;
my $cel;
my $fah;

if ($conv eq 'F-C') {

   $cel = ($temp - 32) * 5/9;
   print "Temperature from $fah degree Fahrenheit is $cel degree Celsius";
}

if ($conv eq 'C-F') {

    $fah = (9 * $temp/5) + 32;
    print "Temperature from $cel degree Celsius is $fah degree Fahrenheit"; 
}
#/usr/bin/perl
严格使用;
使用警告;
打印“输入温度:”;
我的$temp=;
打印“输入要执行的转换:”;
我的$conv=;
我的$cel;
我的$fah;
如果($conv eq‘F-C’){
$cel=($temp-32)*5/9;
打印“温度从$fah华氏度到$cel摄氏度”;
}
如果($conv eq'C-F'){
$fah=(9*$temp/5)+32;
打印“从$cel摄氏度到$fah华氏度的温度”;
}

从键盘输入$temp和$conv后,将显示空白输出。我哪里出错了?请帮忙。提前感谢。

您没有考虑用户输入中的新行字符


中的某些内容分配给每个标量后,调用该标量。

您没有考虑用户输入中的新行字符


中的某些内容分配给每个标量后,调用该标量。

输入后,变量中将有一个换行符。用
chomp
来摆脱它

然后还有第二个问题-您在输出语句中使用了
$fah
$cel
。这应该是
$temp
变量,否则会出现如下错误:

在串联(.)中使用未初始化的值$cel或在

以下是更新的代码:

#!/usr/bin/perl
use strict;
use warnings;
print "Enter the temperature: ";
my $temp = <STDIN>;
chomp($temp);
print "Enter the Conversion to be performed:";
my $conv = <STDIN>;
chomp($conv);
my $cel;
my $fah;
if ($conv eq 'F-C')
{
 $cel = ($temp - 32) * 5/9;
 print "Temperature from $temp degree Fahrenheit is $cel degree Celsius";
}
if ($conv eq 'C-F')
{
 $fah = (9 * $temp/5) + 32;
 print "Temperature from $temp degree Celsius is $fah degree Fahrenheit"; 
}
#/usr/bin/perl
严格使用;
使用警告;
打印“输入温度:”;
我的$temp=;
chomp($temp);
打印“输入要执行的转换:”;
我的$conv=;
chomp($conv);
我的$cel;
我的$fah;
如果($conv eq‘F-C’)
{
$cel=($temp-32)*5/9;
打印“温度从$temp华氏度到$cel摄氏度”;
}
如果($conv eq'C-F')
{
$fah=(9*$temp/5)+32;
打印“温度从$temp摄氏度到$fah华氏度”;
}

输入后,变量中将有一个换行符。用
chomp
来摆脱它

然后还有第二个问题-您在输出语句中使用了
$fah
$cel
。这应该是
$temp
变量,否则会出现如下错误:

在串联(.)中使用未初始化的值$cel或在

以下是更新的代码:

#!/usr/bin/perl
use strict;
use warnings;
print "Enter the temperature: ";
my $temp = <STDIN>;
chomp($temp);
print "Enter the Conversion to be performed:";
my $conv = <STDIN>;
chomp($conv);
my $cel;
my $fah;
if ($conv eq 'F-C')
{
 $cel = ($temp - 32) * 5/9;
 print "Temperature from $temp degree Fahrenheit is $cel degree Celsius";
}
if ($conv eq 'C-F')
{
 $fah = (9 * $temp/5) + 32;
 print "Temperature from $temp degree Celsius is $fah degree Fahrenheit"; 
}
#/usr/bin/perl
严格使用;
使用警告;
打印“输入温度:”;
我的$temp=;
chomp($temp);
打印“输入要执行的转换:”;
我的$conv=;
chomp($conv);
我的$cel;
我的$fah;
如果($conv eq‘F-C’)
{
$cel=($temp-32)*5/9;
打印“温度从$temp华氏度到$cel摄氏度”;
}
如果($conv eq'C-F')
{
$fah=(9*$temp/5)+32;
打印“温度从$temp摄氏度到$fah华氏度”;
}

您也可以像这样尝试
Convert::Pluggable

使用Convert::Pluggable;
my$c=新转换::可插拔;
我的$result=$c->convert({'factor'=>'someNumber','from_unit'=>'c','to_unit'=>'F','precision'=>'somePrecision',});

您也可以像这样尝试
Convert::Pluggable

使用Convert::Pluggable;
my$c=新转换::可插拔;
我的$result=$c->convert({'factor'=>'someNumber','from_unit'=>'c','to_unit'=>'F','precision'=>'somePrecision',});

是。非常感谢。在我为$temp和$conv调用chomp之后,我得到了所需的输出。是的。非常感谢。在我为$temp和$conv调用chomp之后,我得到了所需的输出。如果其他人正在寻找它:有一个CPAN模块可以这样做:如果其他人正在寻找它:有一个CPAN模块可以这样做: