1
我正在嘗試創建一個小的PowerShell程序,它會詢問您一個隨機單詞,您必須對其進行翻譯。我使用兩個數組的索引來確定答案是否正確。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
代碼缺少一些變量標識符:'$ RightTextBox.Text = countercorrect ++'。這些只是一個複製粘貼編輯錯誤? – vonPryz
'$ countercorrect ++' - >'$ script:countercorrect ++','$ counterwrong ++' - >'$ script:counterwrong ++'。增加/減少計數器時,您處於函數的範圍內(未經測試,但我非常有信心)。就像@vonPryz提到的那樣,你錯過了一些'$'符號。 – sodawillow
謝謝!是的,我的錯他們確實複製粘貼錯誤。 @sodawillow非常感謝你使用$ script:countercorrect ++之前試過的解決方案,但是隻能在另一個函數中使用,並且由於某種原因它只是刪除了整個值。但在功能之外使用它就像一個魅力! –