Javascript 需要帮助解决循环问题吗
我最近开始学习Javascript,非常喜欢它。所以,在学习了一些关于循环的信息之后,我决定改进一个简单的石头、剪刀、纸牌游戏。改进是将玩家赢款和计算机赢款作为变量,然后循环函数,直到playerScore变量的值达到10。我还不太擅长语法,尽管我试图了解一般逻辑和我犯了错误的地方 为了达到我的目标,我声明了两个变量——playerScore和computerScore,它们的初始值是0。在每个玩家赢或电脑赢后,我决定给变量加+1 然后开始游戏,我声明了一个函数playGame(),并使用While循环它。循环似乎是无限的,更重要的是,当前的结果并没有记录到控制台。非常感谢您的帮助,这将比我通过的任何课程更能帮助我理解逻辑 代码如下:Javascript 需要帮助解决循环问题吗,javascript,Javascript,我最近开始学习Javascript,非常喜欢它。所以,在学习了一些关于循环的信息之后,我决定改进一个简单的石头、剪刀、纸牌游戏。改进是将玩家赢款和计算机赢款作为变量,然后循环函数,直到playerScore变量的值达到10。我还不太擅长语法,尽管我试图了解一般逻辑和我犯了错误的地方 为了达到我的目标,我声明了两个变量——playerScore和computerScore,它们的初始值是0。在每个玩家赢或电脑赢后,我决定给变量加+1 然后开始游戏,我声明了一个函数playGame(),并使用Whi
var playerScore = 0;
var computerScore = 0;
function getUserChoice() {
var userInput = prompt('Choose stone, scissors or paper');
userInput = userInput.toLowerCase();
if(userInput === 'stone' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
return userInput;
}
else {
alert('Error! Choose stone, scissors or paper!');
}
}
function getComputerChoice() {
var randomNumber = Math.floor(Math.random() *3);
if(randomNumber === 1) {
return 'stone';
}
else if(randomNumber === 2) {
return 'paper';
}
else {
return 'scissors';
}
}
function determineWinner (userChoice, computerChoice) {
if(userChoice === computerChoice) {
return 'That's a tie!';
}
if(userChoice === 'stone') {
if(computerChoice === 'scissors') {
playerScore = playerScore + 1;
сonsole.log(playerScore);
console.log(computerScore);
return 'Player won!';
}
else {
if(computerChoice === 'paper') {
computerScore = computerScore + 1;
сonsole.log(playerScore);
console.log(computerScore);
return 'Computer won!'
}
}
}
if(userChoice === 'paper') {
if(computerChoice === 'scissors') {
computerScore = computerScore + 1;
сonsole.log(playerScore);
console.log(computerScore);
return 'Computer won!';
}
else {
if(computerChoice === 'stone') {
playerScore = playerScore + 1;
сonsole.log(playerScore);
console.log(computerScore);
return 'Player wonи!';
}
}
}
if(userChoice === 'scissors') {
if(computerChoice === 'stone') {
computerScore = computerScore + 1;
сonsole.log(playerScore);
console.log(computerScore);
return 'Computer won!';
}
else {
if(computerChoice === 'paper') {
playerScore = playerScore + 1;
сonsole.log(playerScore);
console.log(computerScore);
return 'Player won!';
}
}
}
if(userChoice === 'bomb') {
if(computerChoice === 'stone' || computerChoice === 'scissors' || computerChoice === 'paper') {
playerScore = playerScore + 1;
сonsole.log(playerScore);
console.log(computerScore);
return 'Player won!';
return 'Player won!';
}
}
}
while(playerScore < 10) {
function playGame() {
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
alert('Player chose' + ' ' + userChoice + '!');
alert('Computer chose' + ' ' + computerChoice + '!');
alert(determineWinner(userChoice, computerChoice));
playGame() = false;
}
};
playGame();
var playerScore=0;
var计算机评分=0;
函数getUserChoice(){
var userInput=prompt('选择石头、剪刀或布');
userInput=userInput.toLowerCase();
如果(userInput=='stone'| | userInput=='paper'| | userInput=='剪刀'| | userInput=='bomb'){
返回用户输入;
}
否则{
警告('错误!选择石头、剪刀或布!');
}
}
函数getComputerChoice(){
var randomNumber=Math.floor(Math.random()*3);
如果(随机数===1){
返回“石头”;
}
否则如果(随机数===2){
返回“文件”;
}
否则{
返回“剪刀”;
}
}
函数确定浏览器(用户选择、计算机选择){
if(userChoice==computerChoice){
返回“打成平局!”;
}
如果(userChoice==='stone'){
如果(computerChoice===‘剪刀’){
playerScore=playerScore+1;
c onsole.log(playerScore);
控制台日志(computerScore);
返回“玩家赢了!”;
}
否则{
如果(计算机选择===‘纸张’){
计算机分数=计算机分数+1;
c onsole.log(playerScore);
控制台日志(computerScore);
返回“计算机赢了!”
}
}
}
如果(userChoice==='paper'){
如果(computerChoice===‘剪刀’){
计算机分数=计算机分数+1;
c onsole.log(playerScore);
控制台日志(computerScore);
返回“计算机赢了!”;
}
否则{
如果(计算机选择==='stone'){
playerScore=playerScore+1;
c onsole.log(playerScore);
控制台日志(computerScore);
返回“玩家赢了!”;
}
}
}
如果(userChoice===‘剪刀’){
如果(计算机选择==='stone'){
计算机分数=计算机分数+1;
c onsole.log(playerScore);
控制台日志(computerScore);
返回“计算机赢了!”;
}
否则{
如果(计算机选择===‘纸张’){
playerScore=playerScore+1;
c onsole.log(playerScore);
控制台日志(computerScore);
返回“玩家赢了!”;
}
}
}
如果(userChoice==='bomb'){
如果(computerChoice==='stone'| | computerChoice==='剪刀'| | computerChoice==='paper'){
playerScore=playerScore+1;
c onsole.log(playerScore);
控制台日志(computerScore);
返回“玩家赢了!”;
返回“玩家赢了!”;
}
}
}
while(playerScore<10){
函数playGame(){
var userChoice=getUserChoice();
var computerChoice=getComputerChoice();
警报('玩家选择'+''+userChoice+'!');
警报('计算机选择'+''+computerChoice+'!');
警报(determineWinner(userChoice,computerChoice));
playGame()=错误;
}
};
游戏();
您需要将while循环移动到函数playGame()
函数游戏(){
while(playerScore<10){
var userChoice=getUserChoice();
var computerChoice=getComputerChoice();
警报('玩家选择'+''+userChoice+'!');
警报('计算机选择'+''+computerChoice+'!');
警报(determineWinner(userChoice,computerChoice));
}
}
非常感谢,成功了!我想做的另一件事是跟踪电脑和播放器的当前分数。在functiondeterminewinner
中,在我添加的每个if/else语句中console.log(playerScore);控制台日志(computerScore)代码>,但它仅在循环完成时向控制台显示结果,而不是“联机”。有什么明显的方法可以解决这个问题吗?你必须给浏览器喘息的时间。为此,您必须删除while
循环,并使用setTimeout
或setInterval
之类的方法。
function playGame() {
while(playerScore < 10) {
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
alert('Player chose' + ' ' + userChoice + '!');
alert('Computer chose' + ' ' + computerChoice + '!');
alert(determineWinner(userChoice, computerChoice));
}
}