PowerShell 计数器不递增
PowerShell counter not incrementing
我正在尝试创建一个小的 powershell 程序,它会随机询问您一个单词,您必须翻译它。我正在使用两个数组的索引来确定答案是否正确。
每次用户输入正确或错误的答案时,文本框都应该增加它的值以显示到目前为止有多少用户是对的和错的。该程序到目前为止可以正常工作,但计数器却没有。此时计数器只增加到 1 并且不会超过 1。我错过了什么吗?
我认为问题可能是 PowerShell 总是调用外部 countercorrect 和 counterwrong 的值,即 0 但我怎么能解决它只调用增量值的问题?
$QuestionArray = New-Object System.Collections.ArrayList
$AnswerArray = New-Object System.Collections.ArrayList
$countercorrect = 0
$counterwrong = 0
#word array
$QuestionArray.Add("word1")
$QuestionArray.Add("word2")
$QuestionArray.Add("word3")
#solution array
$AnswerArray.Add("answer1")
$AnswerArray.Add("answer2")
$AnswerArray.Add("answer3")
#Function to display a new word
function Question {
$global:RandomQuestion = $QuestionArray | Get-Random
$SearchedTextbox.Text = $global:RandomQuestion
}
$InputTextbox.Add_KeyDown({
if ($_.KeyCode -eq "Enter") {
#Get User Guess
$Answer = $InputTextbox.Text
#Get Solution array Index
$IndexPositionQuestion = [array]::indexof($QuestionArray, $global:RandomQuestion)
#Get User answer array Index
$IndexPositionAnswer = [array]::indexof($AnswerArray, $Answer)
#Check if both indexes match
If($IndexPositionAnswer -eq $IndexPositionQuestion){
#this fails / doesn't go above 1
$RightTextBox.Text = countercorrect++
Question
}else{
#this fails / doesn't go above 1
$WrongtTextBox.Text = counterwrong++
Question
}
}
})
我尝试使用单独的函数来增加它的值,但即使这样也只增加到 1。
简化示例:
$form = New-Object System.Windows.Forms.Form
$counter = 0
$button = New-Object System.Windows.Forms.Button
$button.Text = "button"
$button.Add_Click({
$script:counter++
})
$form.Controls.Add($button)
$form.ShowDialog()
$counter
如果将 $script:counter++
替换为 $counter++
,则值不会正确递增。
我正在尝试创建一个小的 powershell 程序,它会随机询问您一个单词,您必须翻译它。我正在使用两个数组的索引来确定答案是否正确。
每次用户输入正确或错误的答案时,文本框都应该增加它的值以显示到目前为止有多少用户是对的和错的。该程序到目前为止可以正常工作,但计数器却没有。此时计数器只增加到 1 并且不会超过 1。我错过了什么吗?
我认为问题可能是 PowerShell 总是调用外部 countercorrect 和 counterwrong 的值,即 0 但我怎么能解决它只调用增量值的问题?
$QuestionArray = New-Object System.Collections.ArrayList
$AnswerArray = New-Object System.Collections.ArrayList
$countercorrect = 0
$counterwrong = 0
#word array
$QuestionArray.Add("word1")
$QuestionArray.Add("word2")
$QuestionArray.Add("word3")
#solution array
$AnswerArray.Add("answer1")
$AnswerArray.Add("answer2")
$AnswerArray.Add("answer3")
#Function to display a new word
function Question {
$global:RandomQuestion = $QuestionArray | Get-Random
$SearchedTextbox.Text = $global:RandomQuestion
}
$InputTextbox.Add_KeyDown({
if ($_.KeyCode -eq "Enter") {
#Get User Guess
$Answer = $InputTextbox.Text
#Get Solution array Index
$IndexPositionQuestion = [array]::indexof($QuestionArray, $global:RandomQuestion)
#Get User answer array Index
$IndexPositionAnswer = [array]::indexof($AnswerArray, $Answer)
#Check if both indexes match
If($IndexPositionAnswer -eq $IndexPositionQuestion){
#this fails / doesn't go above 1
$RightTextBox.Text = countercorrect++
Question
}else{
#this fails / doesn't go above 1
$WrongtTextBox.Text = counterwrong++
Question
}
}
})
我尝试使用单独的函数来增加它的值,但即使这样也只增加到 1。
简化示例:
$form = New-Object System.Windows.Forms.Form
$counter = 0
$button = New-Object System.Windows.Forms.Button
$button.Text = "button"
$button.Add_Click({
$script:counter++
})
$form.Controls.Add($button)
$form.ShowDialog()
$counter
如果将 $script:counter++
替换为 $counter++
,则值不会正确递增。