为什么以下while语句永远循环?我想在用户输入"是"或"否"后突破.其他一切都应该让他留在输入循环中.
while ($input -ne "yes" -or $input -ne "no") { $input = Read-Host "Ready to process? [yes|no]" switch ($input) { yes { write-host "yes" } no { write-host "no" } default { write-host "input not understood" } } }
Bathsheba.. 7
让我们解开逻辑:
1)如果输入为"是",则它不等于"否".
2)如果输入为"否",则它不等于"是".
3)如果它既不是"是"也不是"否",那么它不等于"是"或"否".
因此while ($input -ne "yes" -or $input -ne "no") {
总是如此.所以它永远循环.
我想你的意思是
while ($input -ne "yes" -and $input -ne "no") {
有趣的是,你在这里可以用德摩根定律表达.见http://en.wikipedia.org/wiki/De_Morgan%27s_laws
让我们解开逻辑:
1)如果输入为"是",则它不等于"否".
2)如果输入为"否",则它不等于"是".
3)如果它既不是"是"也不是"否",那么它不等于"是"或"否".
因此while ($input -ne "yes" -or $input -ne "no") {
总是如此.所以它永远循环.
我想你的意思是
while ($input -ne "yes" -and $input -ne "no") {
有趣的是,你在这里可以用德摩根定律表达.见http://en.wikipedia.org/wiki/De_Morgan%27s_laws