【问题标题】:Tie result cannot be outputted平局结果无法输出
【发布时间】:2020-04-17 05:27:01
【问题描述】:

在创建 rock,paper,cissors 应用程序时,我的任务是在控制台中输出特定结果,例如输出纸-纸并接收“It's a tie”结果。好吧,我试图这样做,但由于某种原因,我不能这样做。代码如下:

const user = userInput => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('error');
  }
} 
function computer() {
  random = Math.floor(Math.random() * 3);
  switch(random){
    case 0 : return 'rock';
    break;
    case 1 : return 'paper';
    break;
    default : return 'scissors';
  }
};
const winner = () => {
  const choice1 = user('rock');
  const choice2 = computer();
  if(choice1 === choice2) {
    return 'It\'s a tie!';
  }
 if(choice1 === 'rock') {
  if(choice2 === 'paper') {
    return 'Computer wins!';
  } else {
    return 'You win!';
  }
}
 if(choice1 === 'paper') {
  if(choice2 === 'scissors') {
    return 'Computer wins!';
  } else {
    return 'You win!';
  }
}
 if(choice1 === 'scissors') {
  if(choice2 === 'rock') {
    return 'Computer wins!';
  } else {
    return 'You win!';
  }
}
}
console.log(winner('paper', 'paper'));

例如,当我尝试输入 console.log(winner('paper', 'paper')) 时,结果是随机的,这意味着它可能是赢、输或平局,因为事实(或者我认为是原因)计算机的选择是随机的,但这就是我被要求创建计算机选择的方式。如果我输入 console.log(('paper', 1)) 我会得到相同的结果,但不知何故,任务要求我使用 console.log(winner('paper', 'paper')) 并告诉我结果应该是平局。我也试过让用户在choice1中没有参数,但由于用户的选择中没有输入,我得到一个错误。

任务如下:

console.log(determineWinner('paper', 'scissors')); // prints something like 'The computer won!'
console.log(determineWinner('paper', 'paper')); // prints something like 'The game is a tie!'
console.log(determineWinner('paper', 'rock')); // prints something like 'The user won!'

任何帮助或想法将不胜感激。谢谢!

【问题讨论】:

  • 您正在使用两个参数调用您的函数winner,但声明winner() 时没有任何参数。你需要用类似这样的东西重新定义你的功能winner = (userChoise, computerChoice) =>{}

标签: javascript


【解决方案1】:

这样的事情会奏效。如果这是你需要的。

const user = userInput => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('error');
  }
}
function computer() {
  const random = Math.floor(Math.random() * 3);
  switch(random){
    case 0 : return 'rock';
      break;
    case 1 : return 'paper';
      break;
    default : return 'scissors';
  }
};


// Here is where you need to change something
const determineWinner = (userChoice, computerChoice) => {
// And use the parameters here:
  const choice1 = userChoice;
  const choice2 = computerChoice;
  if (choice1 === choice2) {
    return 'It\'s a tie!';
  }
  if (choice1 === 'rock') {
    if (choice2 === 'paper') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  }
  if (choice1 === 'paper') {
    if (choice2 === 'scissors') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  }
  if (choice1 === 'scissors') {
    if (choice2 === 'rock') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  }
};

const userInput = 'rock';
console.log(determineWinner(user(userInput), computer()));
console.log(determineWinner('rock', 'paper'));
console.log(determineWinner('paper', 'paper'));

// For you requirement:
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('paper', 'paper')); 
console.log(determineWinner('paper', 'rock')); 

但是,如果您按照您的要求对参数进行硬编码,那么您的函数 usercomputer 将不会被使用。

【讨论】:

  • 谢谢!删除获胜者函数中的前 2 个 const 并创建参数帮助我完成了任务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
相关资源
最近更新 更多