需要 powershell 脚本方面的帮助
请参阅下面的代码 ##如果用户输入 %100 或 a100 或非数字值作为 temp,我需要显示“错误:您必须输入数字值!” Write-host $("错误:您必须输入数值!") ## 然后返回开始 ##im 猜测 if ($value isnot [int]) 或类似的东西才能工作。谢谢。 ##如果没有正确编码,我会遇到错误......华氏温度是多少:a111 ##无法将值“a111”转换为类型“System.Single”。错误:“输入字符串的格式不正确。”
Write-Host("="*31)
Write-Host $("Fahrenheit to Celsius Converter")
Write-Host $("Script Written By Jesse ")
Write-Host("="*31)
$value = (Read-Host("What is the temperature in Fahrenheit"))
$fahr = (($value -32) /9) *5
Write-Host $("Fahrenheit", $value, "is equal to Celsius:", [Math]::Round($fahr,3))
$input = (Read-Host("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host ("="*31)
while ($input)
{
if ($input -eq 1)
{
Write-Host $("Fahrenheit to Celsius Converter")
Write-Host $("Script Written By Jesse Nieto ")
Write-Host ("="*31)
$value = (Read-Host("What is the temperature in Fahrenheit"))
$fahr = (($value -32) /9) *5
Write-Host $("Fahrenheit", $value ,"is equal to Celsius:" ,[Math]::Round($fahr,3))
$input = (Read-Host("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host("="*31)
}
elseif ($input -eq 0)
{
Write-Host $("Thank You. Bye! ")
break
}
else
{
Write-Host $("Please enter a valid option! ")
$input = (Read-Host ("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host ("="*31)
}
}
See code below
##If user inputs %100 or a100 or non-numeric value for temp, I need to display "Error: You must enter a numeric Value!" Write-host $("Error: You must enter a numeric Value!")
## then goes back to start
##im guessing if ($value isnot [int]) or something like that for it to work. thank you.
##Error I get without proper coding......What is the temperature in Fahrenheit: a111
##Cannot convert value "a111" to type "System. Single". Error: "Input string was not in a ##correct format."
Write-Host("="*31)
Write-Host $("Fahrenheit to Celsius Converter")
Write-Host $("Script Written By Jesse ")
Write-Host("="*31)
$value = (Read-Host("What is the temperature in Fahrenheit"))
$fahr = (($value -32) /9) *5
Write-Host $("Fahrenheit", $value, "is equal to Celsius:", [Math]::Round($fahr,3))
$input = (Read-Host("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host ("="*31)
while ($input)
{
if ($input -eq 1)
{
Write-Host $("Fahrenheit to Celsius Converter")
Write-Host $("Script Written By Jesse Nieto ")
Write-Host ("="*31)
$value = (Read-Host("What is the temperature in Fahrenheit"))
$fahr = (($value -32) /9) *5
Write-Host $("Fahrenheit", $value ,"is equal to Celsius:" ,[Math]::Round($fahr,3))
$input = (Read-Host("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host("="*31)
}
elseif ($input -eq 0)
{
Write-Host $("Thank You. Bye! ")
break
}
else
{
Write-Host $("Please enter a valid option! ")
$input = (Read-Host ("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host ("="*31)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的脚本中有很多可以简化的冗余代码,最大的问题是使用
$input
这是一个自动变量,不应手动分配。至于从华氏温度转换为摄氏度的公式,您可以使用 函数 为此,除此之外,根据 Google 的说法,公式应该是(X − 32) × 5 / 9
我不是这方面的专家,请随意更改为您喜欢的内容。There is much redundant code on your script which could be simplified, the biggest issue is the use of
$input
which is an automatic variable and should not be assigned manually. As for the formula to convert from Fahrenheit to Celsius, you could use a function for that, as aside, according to Google, the formula should be(X − 32) × 5 / 9
though I'm not an expert on this, feel free to change for whatever you like.