为什么我的循环只发生一次?我想输入5个用户并将其添加到哈希表中,但代码运行一次然后停止。我该如何解决?
$userID=""
$firstname=""
$lastname="
$personCount = 1
$personHash = @{}
while ($personCount -le 5){
$personCount++
While([string]::IsNullOrWhiteSpace($userID)){
$userID = Read-Host "Enter ID"
}
While([string]::IsNullOrWhiteSpace($firstname)){
$firstname = Read-Host "Enter First Name"
}
While([string]::IsNullOrWhiteSpace($lastname)){
$lastname = Read-Host "Enter Last Name"
}
$user = New-Object PSCustomObject -Property @{
ID = $userID
FirstName = $firstname
LastName = $lastname
}
$personHash.Add($user.ID,$user)
}
}
输出:
Enter ID: 1001
Enter First Name: Charles
Enter Last Name: Whitfield
Name Value
---- -----
1001 @{ID=1001; FirstName=Charles; LastName=Whitfield}
1001 @{ID=1001; FirstName=Charles; LastName=Whitfield}
1001 @{ID=1001; FirstName=Charles; LastName=Whitfield}
1001 @{ID=1001; FirstName=Charles; LastName=Whitfield}
答案 0 :(得分:1)
你的循环运行5次。
问题是,一旦变量在第一个循环中填充,它们就会在所有后续循环中失败。它运行一次,执行读取主机并设置变量。然后再运行4次,但找不到任何事情。
您的更新代码更近了。您保存数据,但没有清除下一个循环的变量。
$userID=""
$firstname=""
$lastname=""
$personCount = 1
$personHash = @{}
while ($personCount -le 5){
$personCount++
While([string]::IsNullOrWhiteSpace($userID)){
$userID = Read-Host "Enter ID"
}
While([string]::IsNullOrWhiteSpace($firstname)){
$firstname = Read-Host "Enter First Name"
}
While([string]::IsNullOrWhiteSpace($lastname)){
$lastname = Read-Host "Enter Last Name"
}
$user = New-Object PSCustomObject -Property @{
ID = $userID
FirstName = $firstname
LastName = $lastname}
$personHash.Add($user.ID,$user)
$UserID,$firstname,$lastname = ""
}
答案 1 :(得分:1)
这里有几个问题,while循环需要明确的变量,如mjolinor所说。完成后,我建议将数据保存在一个数组中,这样每个用户的数据都不会在下一次迭代中丢失:
$users = @()
$personCount = 1
while ($personCount -le 5){
$userID=""
$firstname=""
$lastname=""
$personCount++
While([string]::IsNullOrWhiteSpace($userID)){
$userID = Read-Host "Enter ID"
}
While([string]::IsNullOrWhiteSpace($firstname)){
$firstname = Read-Host "Enter First Name"
}
While([string]::IsNullOrWhiteSpace($lastname)){
$lastname = Read-Host "Enter Last Name"
}
$users += New-Object -TypeName PSCustomObject -Property @{
userID = $userID;
firstName = $firstname;
lastName = $lastname
}
}
数组$ users包含所有数据后,您可以使用$users
或具有$users[0]
,$users[1]
等的特定用户一次性输出所有值。
答案 2 :(得分:1)
您发布的代码显然不可能创建您发布的输出。即使我对这两个语法错误进行了折扣,您仍然会获得4个关键冲突异常,因为您尝试将相同的密钥添加到同一个哈希表中5次(哈希表中的密钥必须是唯一的)。
如果要在哈希表中创建5个用户对象,并确保用户为所有3个属性输入值,如果您希望重复ID引发错误,则可以执行类似这样的操作:
$personHash = @{}
1..5 | % {
do {
$userID = Read-Host "Enter ID"
$firstname = Read-Host "Enter First Name"
$lastname = Read-Host "Enter Last Name"
} until ( $userID -and $firstname -and $lastname )
$user = New-Object -Type PSCustomObject -Property @{
'ID' = $userID
'FirstName' = $firstname
'LastName' = $lastname
}
$personHash.Add($userID, $user)
}
或者像这样,如果你想要一个重复的ID来更新当前值:
$personHash = @{}
1..5 | % {
do {
$userID = Read-Host "Enter ID"
$firstname = Read-Host "Enter First Name"
$lastname = Read-Host "Enter Last Name"
} until ( $userID -and $firstname -and $lastname )
$user = New-Object -Type PSCustomObject -Property @{
'ID' = $userID
'FirstName' = $firstname
'LastName' = $lastname
}
$personHash[$userID] = $user
}