hashtable.ContainsKey 永远不会计算为真?
hashtable.ContainsKey never evaluates to true?
我有以下代码读取 XML 文件,输出节点并提示用户输入选择。出于某种原因,无论我输入什么,它总是显示 "Invalid Choice"。我做错了什么?
$buildsFile = [System.Xml.XmlDocument](Get-Content "$($config.remotebuildspath)/builds.xml");
$builds = $buildsFile.builds;
$i = 0
$buildHash = @{}
Write-Host "Available Builds: " -ForeGroundColor Green
ForEach ($buildVersion in $builds.ChildNodes) {
$i++
Write-Host "(" -NoNewline -ForeGroundColor Green
Write-Host $i -NoNewline
Write-Host ") $($buildVersion.LocalName)" -ForeGroundColor Green
$buildHash.add($i, $buildVersion.LocalName)
}
$isValid = $false
do {
Write-Host "Choose: " -NoNewline -ForeGroundColor Green
$choice = Read-Host
if ($buildHash.ContainsKey($choice)) {
$isValid = $true
} else {
Write-Host "Invalid Choice!" -ForeGroundColor Red
$isValid = $false
}
} while ($isValid -eq $false)
Write-Host "You picked ${$buildHash[$choice]}"
调试器截图:
根据@Jeroen Mostert 的评论,我在 if
语句中添加了 [int]$choice
。为了避免在非数字输入时引发错误,我还合并了 this answer 中的辅助函数 Is-Numeric
。
这是我的最终代码:
function Is-Numeric ($Value) {
return $Value -match "^[\d\.]+$"
}
$buildsFile = [System.Xml.XmlDocument](Get-Content "$($config.remotebuildspath)/builds.xml");
$builds = $buildsFile.builds;
$i = 0
$buildHash = @{}
Write-Host "Available Builds: " -ForeGroundColor Green
ForEach ($buildVersion in $builds.ChildNodes) {
$i++
Write-Host "(" -NoNewline -ForeGroundColor Green
Write-Host $i -NoNewline
Write-Host ") $($buildVersion.LocalName)" -ForeGroundColor Green
$buildHash.add($i, $buildVersion.LocalName)
}
$isValid = $false
do {
Write-Host "Choose: " -NoNewline -ForeGroundColor Green
$choice = Read-Host
if (Is-Numeric $choice) {
if ($buildHash.ContainsKey([int]$choice)) {
$isValid = $true
} else {
Write-Host "Invalid Choice!" -ForeGroundColor Red
$isValid = $false
}
} else {
$isValid = $false
Write-Host "Please enter a number!" -ForegroundColor Red
}
} while ($isValid -eq $false)
Write-Host "You picked ${$buildHash[$choice]}"
我有以下代码读取 XML 文件,输出节点并提示用户输入选择。出于某种原因,无论我输入什么,它总是显示 "Invalid Choice"。我做错了什么?
$buildsFile = [System.Xml.XmlDocument](Get-Content "$($config.remotebuildspath)/builds.xml");
$builds = $buildsFile.builds;
$i = 0
$buildHash = @{}
Write-Host "Available Builds: " -ForeGroundColor Green
ForEach ($buildVersion in $builds.ChildNodes) {
$i++
Write-Host "(" -NoNewline -ForeGroundColor Green
Write-Host $i -NoNewline
Write-Host ") $($buildVersion.LocalName)" -ForeGroundColor Green
$buildHash.add($i, $buildVersion.LocalName)
}
$isValid = $false
do {
Write-Host "Choose: " -NoNewline -ForeGroundColor Green
$choice = Read-Host
if ($buildHash.ContainsKey($choice)) {
$isValid = $true
} else {
Write-Host "Invalid Choice!" -ForeGroundColor Red
$isValid = $false
}
} while ($isValid -eq $false)
Write-Host "You picked ${$buildHash[$choice]}"
调试器截图:
根据@Jeroen Mostert 的评论,我在 if
语句中添加了 [int]$choice
。为了避免在非数字输入时引发错误,我还合并了 this answer 中的辅助函数 Is-Numeric
。
这是我的最终代码:
function Is-Numeric ($Value) {
return $Value -match "^[\d\.]+$"
}
$buildsFile = [System.Xml.XmlDocument](Get-Content "$($config.remotebuildspath)/builds.xml");
$builds = $buildsFile.builds;
$i = 0
$buildHash = @{}
Write-Host "Available Builds: " -ForeGroundColor Green
ForEach ($buildVersion in $builds.ChildNodes) {
$i++
Write-Host "(" -NoNewline -ForeGroundColor Green
Write-Host $i -NoNewline
Write-Host ") $($buildVersion.LocalName)" -ForeGroundColor Green
$buildHash.add($i, $buildVersion.LocalName)
}
$isValid = $false
do {
Write-Host "Choose: " -NoNewline -ForeGroundColor Green
$choice = Read-Host
if (Is-Numeric $choice) {
if ($buildHash.ContainsKey([int]$choice)) {
$isValid = $true
} else {
Write-Host "Invalid Choice!" -ForeGroundColor Red
$isValid = $false
}
} else {
$isValid = $false
Write-Host "Please enter a number!" -ForegroundColor Red
}
} while ($isValid -eq $false)
Write-Host "You picked ${$buildHash[$choice]}"