java 如何让这个循环/函数正确运行?

h4cxqtbf  于 2023-05-21  发布在  Java
关注(0)|答案(1)|浏览(121)

所以我在做一个石头剪刀布游戏,用提示符来玩
这是整个代码

var options = ['R','P','S','r','p','s']
var userRes;
var checkVar;
var compChoice;
var checkStat;

var winStat = 0;
var lossStat = 0;
var tieStat = 0;

function isValid() {
    for (var i = 0; i < options.length; i++) {
        const found = options[i];
        if (userRes === found) {
            return checkVar = true;
        } else if (userRes !== found) {
            return checkVar = false;
        }
    }
}

function getCompChoice() {
    let compSet = options[Math.floor(Math.random() * options.length)];
    compChoice = compSet.toUpperCase();
    console.log(compChoice)
    return alert('The computer chose ' + compChoice);
}

function getUserChoice () {
    userSet = prompt('Rock Paper or Scissors?');
    if (userSet === null) {
        return startGame();
    } else {
        userRes = userSet.toUpperCase();
    }
    isValid()
    if (checkVar === true) {

        console.log('continue')
        userRes.toUpperCase();
        console.log(userRes);

        getCompChoice()

        if((userRes === 'R' && compChoice === 'S') ||
        (userRes === 'P' && compChoice === 'R' ||
        (userRes === 'S' && compChoice === 'P'))) {
            console.log('win')
            alert('You Won !')
            checkStat = true
            playAgain()
        } else if (userRes === compChoice) {
            console.log('tie')
            alert('You Tied !')
            checkStat = null
            playAgain()
        } else if (userRes !== compChoice) {
            console.log('loss')
            alert('You Lost !')
            checkStat = false
            playAgain()
        }
    } else if (checkVar === false) {
        console.log('end')
        console.log(userRes);

        alert('Please enter R, P, or S. (Not case sensitive).');
        getUserChoice();
    }

}

function playAgain() {
    if (checkStat) {
        winStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    } else if (checkStat === null){
        tieStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    } else if (!checkStat) {
        lossStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    }

    pAgain = confirm('Play Again ?')
    if (pAgain) {
        getUserChoice();
    }
}

function startGame () {
    askUser = confirm('Would you like to play a game of Rock, Paper, Scissors ?')
    if (askUser) {
        return getUserChoice();
    } else if (!askUser) {
        return alert('Come back next time !')
    }
}

startGame();

现在我这里的问题是,一切都按预期工作,但函数isValid()只循环一次,只输出'R'到我的控制台日志,这完全破坏了游戏,我只能使用r来玩使用'S'或'P'返回警报说我没有输入r p或s,如果有人可以帮助我理解为什么循环只运行一次或只输出'R'那就太好了,我被这个问题难倒了一段时间
只是一个fyi我仍然是相当新的javascript
我尝试使用const在控制台中将数组更改为字符串输出,但也没有任何效果。

fwzugrvs

fwzugrvs1#

问题是你回来得早!
这就像说:

function isValid() {
  return checkVar = (userRes === options[0]);
}

因此,解决方案是在返回之前检查所有结果:

function isValid() {
    for (var i = 0; i < options.length; i++) {
        const found = options[i];
        if (userRes === found) {
            return checkVar = true;
        }
    }

    checkVar = false;
}
var options = ['R','P','S','r','p','s']
var userRes;
var checkVar;
var compChoice;
var checkStat;

var winStat = 0;
var lossStat = 0;
var tieStat = 0;

function isValid() {
    for (var i = 0; i < options.length; i++) {
        const found = options[i];
        if (userRes === found) {
            return checkVar = true;
        }
    }
    
    checkVar = false;
}

function getCompChoice() {
    let compSet = options[Math.floor(Math.random() * options.length)];
    compChoice = compSet.toUpperCase();
    console.log(compChoice)
    return alert('The computer chose ' + compChoice);
}

function getUserChoice () {
    userSet = prompt('Rock Paper or Scissors?');
    if (userSet === null) {
        return startGame();
    } else {
        userRes = userSet.toUpperCase();
    }
    isValid()
    if (checkVar === true) {

        console.log('continue')
        userRes.toUpperCase();
        console.log(userRes);

        getCompChoice()

        if((userRes === 'R' && compChoice === 'S') ||
        (userRes === 'P' && compChoice === 'R' ||
        (userRes === 'S' && compChoice === 'P'))) {
            console.log('win')
            alert('You Won !')
            checkStat = true
            playAgain()
        } else if (userRes === compChoice) {
            console.log('tie')
            alert('You Tied !')
            checkStat = null
            playAgain()
        } else if (userRes !== compChoice) {
            console.log('loss')
            alert('You Lost !')
            checkStat = false
            playAgain()
        }
    } else if (checkVar === false) {
        console.log('end')
        console.log(userRes);

        alert('Please enter R, P, or S. (Not case sensitive).');
        getUserChoice();
    }

}

function playAgain() {
    if (checkStat) {
        winStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    } else if (checkStat === null){
        tieStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    } else if (!checkStat) {
        lossStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    }

    pAgain = confirm('Play Again ?')
    if (pAgain) {
        getUserChoice();
    }
}

function startGame () {
    askUser = confirm('Would you like to play a game of Rock, Paper, Scissors ?')
    if (askUser) {
        return getUserChoice();
    } else if (!askUser) {
        return alert('Come back next time !')
    }
}

startGame();

相关问题