gpt4 book ai didi

function - PowerShell 函数来求一个数 : 的平方

转载 作者:行者123 更新时间:2023-12-04 17:32:49 25 4
gpt4 key购买 nike

这是我用于查找值的平方的函数:

function Get-Square($value)
{
$result = $value * $value
return $result
}

$value = Read-Host 'Enter a value'
$result = Get-Square $value
Write-Output "$value * $value = $result"

PS C:\Users> .\Get-Time.ps1
Enter a value: 4
4 * 4 = 4444


为什么结果是 4444 而不是 16?谢谢你。

最佳答案

除了 Alistair 关于转换的回答,string返回者 Read-Hostint ,您可能想要使用 Math 库来平方该值。

示例代码

function Get-Square([int] $value)
{
$result = [Math]::Pow($value,2)
return $result
}

$value = Read-Host 'Enter a value'
$result = Get-Square $value
Write-Output "$value * $value = $result"

结果

Enter a value: 4
4 * 4 = 16

关于function - PowerShell 函数来求一个数 : 的平方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37558911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com