gpt4 book ai didi

powershell - 如果语句不起作用则嵌套

转载 作者:行者123 更新时间:2023-12-03 00:28:47 24 4
gpt4 key购买 nike

我正在尝试编写用于查找a年的代码。但是,嵌套的ifelse格式似乎有问题。请提出建议。

[int]$x = Read-Host -Prompt 'Input a year : ';

if ($x % 4 -eq 0) {
Write-Host $x 'is a leap year';
if ($x % 100 -eq 0) {
Write-Host $x 'is a leap year';
if ($x % 400 -eq 0) {
Write-Host $x 'is a leap year';

else {
Write-Host $x 'is not a leap year';
}
}
else {
Write-Host $x 'is not a leap year';
}
}
else {
Write-Host $x 'is not a leap year';
}

最佳答案

我想这就是你想要的:

[int]$x = Read-Host -Prompt 'Input a year : ';

If ($x % 4 -eq 0) {
if ($x % 400 -eq 0){
Write-Host $x 'is a leap year';
}
Elseif ($x % 100 -eq 0){
Write-Host $x 'is not a leap year';
}
Else{
Write-Host $x 'is a leap year';
}
}
Else {
Write-Host $x 'is not a leap year';
}

关于powershell - 如果语句不起作用则嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36965023/

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