需要 powershell 脚本方面的帮助

发布于 2025-01-10 21:25:12 字数 1502 浏览 0 评论 0原文

请参阅下面的代码 ##如果用户输入 %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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尸血腥色 2025-01-17 21:25:13

您的脚本中有很多可以简化的冗余代码,最大的问题是使用 $input 这是一个自动变量,不应手动分配。至于从华氏温度转换为摄氏度的公式,您可以使用 函数 为此,除此之外,根据 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)
}

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.

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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文