退出Javascript中的函数。简单的石头,布,剪刀游戏
如果userChoice为null或除rock、paper或剪刀之外的单词,如何停止函数的执行。 我试着用return,但没法用。 感谢您的帮助。 谢谢 JS Fiddle链接=退出Javascript中的函数。简单的石头,布,剪刀游戏,javascript,Javascript,如果userChoice为null或除rock、paper或剪刀之外的单词,如何停止函数的执行。 我试着用return,但没法用。 感谢您的帮助。 谢谢 JS Fiddle链接= var userChoice=prompt('您选择石头、布还是剪刀?'); var compChoice=Math.random(); 如果(compChoice只需执行返回; function compare() { if(!userChoice) return; // (...) more co
var userChoice=prompt('您选择石头、布还是剪刀?');
var compChoice=Math.random();
如果(compChoice只需执行返回;
function compare() {
if(!userChoice)
return;
// (...) more code
}
return
调用退出函数。但请注意,如果语句中的条件错误,则只能在函数中使用return
if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors')
function compare() {
if (userChoice === null) {
console.log('Please select an option');
return;
} else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') {
console.log('Please select rock, paper or scissors');
return;
}
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
}
e1&&e2&&e3&&……
形式的表达式计算为最后一个eN
子表达式,如果所有表达式都真实。因此,您的测试等效于:
if (userChoice !== 'scissors')
您应该在显示游戏结果之前进行检查,然后从函数返回。因此应该是:
if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors')
function compare() {
if (userChoice === null) {
console.log('Please select an option');
return;
} else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') {
console.log('Please select rock, paper or scissors');
return;
}
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
}
如果我理解正确,您希望返回到代码的开头。您应该将整个内容包装在一个函数中,然后再次调用该函数:
function rockPaperScissors() {
var userChoice = prompt('Do you choose rock, paper or scissors?');
var compChoice = Math.random();
if (compChoice <= 0.34) {
compChoice = 'rock';
} else if (compChoice <= 0.67) {
compChoice = 'paper';
} else {
compChoice = 'scissors';
}
function compare() {
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
if (userChoice === null) {
console.log('Please select an option');
rockPaperScissors();
} else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') { // fixed
console.log('Please select rock, paper or scissors');
rockPaperScissors();
}
}
console.log('Your choice = ' + userChoice);
console.log('Computer Choice = ' + compChoice);
compare();
}
rockPaperScissors();
功能:剪纸机(){
var userChoice=prompt('您选择石头、布还是剪刀?');
var compChoice=Math.random();
if(compChoice我更新了函数的每个if/else部分,当它将if/else条件与返回false
匹配时退出。此外,我将if(userChoice==null)
检查改为if(userChoice==”)
,因为它看起来像提示符(“您选择石头、布或剪刀吗?”);
在用户未输入任何值时返回一个空字符串。此外,我刚刚根据**Barmar**的建议更新了函数的开头以使其工作(他第一次就正确了)
你想在哪里退出该函数?return
应该可以工作。在if语句中,userChoice==null和when userChoice!='rock'&&&'paper'&&&'scissors'感谢你发现了这个错误。是的,我刚刚移动了我的代码,它工作了!非常感谢你。我尝试将整个东西包装在一个函数中,但它没有执行co由于某种原因。谢谢你帮我解决了这个错误。谢谢。哦,是的,你也需要调用这个函数。(哦,是的,我错过了最后的调用哈哈。谢谢:)