每当我运行以下代码时,我都会不断收到“您选择了石头,您选择了纸,您选择了剪刀”,但我不知道为什么。
//if they haven't picked 1, 2, or 3, ask for input until they do
while (userSELECT != 1 && userSELECT != 2 && userSELECT != 3) {
var userSELECT = prompt("1 = Rock | 2 = Paper | 3 = Scissors", "<>");
};
// 1 is rock
if (userSELECT = '1') {
console.log("you chose Rock")
};
// 2 is paper
if (userSELECT = '2') {
console.log("you chose Paper")
};
// 3 is scissors
if (userSELECT = '3') {
console.log("you chose Scissors")
};
最佳答案
=
用于设置值,而不是用于比较,因此应使用==
而不是=
:
//if they haven't picked 1, 2, or 3, ask for input until they do
while (userSELECT != 1 && userSELECT != 2 && userSELECT != 3) {
var userSELECT = prompt("1 = Rock | 2 = Paper | 3 = Scissors", "<>");
};
// 1 is rock
if (userSELECT == '1') {
console.log("you chose Rock")
};
// 2 is paper
if (userSELECT == '2') {
console.log("you chose Paper")
};
// 3 is scissors
if (userSELECT == '3') {
console.log("you chose Scissors")
};
关于javascript - if语句出现问题,用于剪刀石头布程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61613585/