需要有关 powershell 脚本的帮助
Need assistance with powershell script
见下面的代码
##If user inputs %100 or a100 or non-numeric value for temp,我需要显示“错误:你必须输入一个数值!” Write-host $("错误:您必须输入数值!")
## 然后返回开始
##我在猜测是否($value isnot [int])或类似的东西让它工作。谢谢你。
##Error 我没有正确编码......华氏温度是多少: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)
}
}
你的脚本有很多多余的代码可以简化,最大的问题是使用$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,顺便说一句,根据Google,公式应该是(X − 32) × 5 / 9
虽然我不是这方面的专家,但请随意更改任何你喜欢的东西。
function ConvertTo-Celsius {
param([int]$Value)
($value - 32) * 5 / 9
}
:outer while($true) {
$value = Read-Host "What is the temperature in Fahrenheit"
try {
$celsius = ConvertTo-Celsius $value
}
catch {
# If input was invalid restart the loop
Write-Host "Invalid input, only integers allowed!"
continue
}
Write-Host "Fahrenheit $value°F is equal to $([math]::Round($celsius, 3))°C"
do {
$shouldExit = Read-Host "Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"
switch($shouldExit) {
0 {
Write-Host "Thank You. Bye!"
break outer
}
1 { continue }
Default { Write-Host "Please enter a valid option!" }
}
} until($shouldExit -eq 1)
}
见下面的代码 ##If user inputs %100 or a100 or non-numeric value for temp,我需要显示“错误:你必须输入一个数值!” Write-host $("错误:您必须输入数值!") ## 然后返回开始 ##我在猜测是否($value isnot [int])或类似的东西让它工作。谢谢你。 ##Error 我没有正确编码......华氏温度是多少: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)
}
}
你的脚本有很多多余的代码可以简化,最大的问题是使用$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,顺便说一句,根据Google,公式应该是(X − 32) × 5 / 9
虽然我不是这方面的专家,但请随意更改任何你喜欢的东西。
function ConvertTo-Celsius {
param([int]$Value)
($value - 32) * 5 / 9
}
:outer while($true) {
$value = Read-Host "What is the temperature in Fahrenheit"
try {
$celsius = ConvertTo-Celsius $value
}
catch {
# If input was invalid restart the loop
Write-Host "Invalid input, only integers allowed!"
continue
}
Write-Host "Fahrenheit $value°F is equal to $([math]::Round($celsius, 3))°C"
do {
$shouldExit = Read-Host "Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"
switch($shouldExit) {
0 {
Write-Host "Thank You. Bye!"
break outer
}
1 { continue }
Default { Write-Host "Please enter a valid option!" }
}
} until($shouldExit -eq 1)
}