【问题标题】:I have trouble managing my for loop inside while loo[ and vice versa我无法在 while loo [ 中管理我的 for 循环,反之亦然
【发布时间】:2021-12-12 13:08:34
【问题描述】:

我最近一直在做一个项目,在 JS 控制台中制作剪刀石头布游戏。现在,正如您在下面的代码中看到的那样,我只是用石头和剪刀测试水域,一旦我解决了这个错误,很快就会添加纸张。我想在随机生成的选择(变量 b)和用户的选择(变量 a)之间玩这个游戏六轮。

我希望 for 循环运行 6 次以玩 6 轮。目前没有任何计分器。当我运行 for 循环时,我希望将随机生成的选择登录到控制台,然后将要求用户进行选择,之后将记录他/她的选择。然后,我希望 do-while 循环中的 if 语句评估 a 和 b 的当前两个值,并在记录两个响应后立即给出我试图记录的结果(例如,岩石压碎剪刀!你松了!) .然后将生成并记录另一个随机选择,依此类推......

现在发生的情况是,我仅在 for 循环的第六次迭代之后才评估 if 语句,而它应该在每次迭代后给我来自 if 条件的记录语句。

代码如下:

var arr = ['rock', 'scissor']
let win = false

function play(arr){

  
    
    do{

      for(let i=0; i<=5;i++){
        var a = prompt('Enter choice:')
        console.log(a)
        var b = arr[(Math.floor(Math.random()*2))]
        console.log(b)

        if(a==="rock" && b==="scissor"){
          console.log("Rock crushes Scissor! You win!") 
          win=true
        }
        
        else if(b==="rock" && a==="scissor"){
          console.log("Rock crushes Scissor! You lose!") 
          win=true
        }
    
      }

    }
    while(!win);
  
    check_winner(win)
  }

 
function check_winner(win){
  if(win===true){
    console.log("gamne over")
  }
  else{
    prompt("Enter again")
  }
}

play(arr)

我是 JS 新手,很难处理这个错误。也许你们真诚的人可以让我摆脱这种困境。任何帮助将不胜感激。谢谢!

【问题讨论】:

  • win=false win 在你丢失的情况下应该是假的
  • 另外,check_winner(win) 在循环之外,这就是为什么它只在循环结束后执行一次。
  • @sojin 谢谢您的帮助先生。但是你能指定哪个条件吗?循环中的else if条件还是check_winner函数中的条件?
  • @aerial301 感谢您的关注,先生。它正在按预期工作!这是我第一次在这里发布问题,我很喜欢您的所有反馈。干杯好先生!
  • do{ }while(!win) 循环将一直运行直到 win 为真,因此下面的 check_winner() 仅在 win=true 时执行。那么我认为你应该描述你的目标逻辑。具体什么时候赢?什么时候赢了5次中的3次?还是只是一次尝试,选择“摇滚”的玩家获胜?

标签: javascript arrays loops if-statement variables


【解决方案1】:

除了cmets中提到的东西

  • win = false 如果输了
  • check_winner(win) 在 do while 循环之外,所以只在 win=true 时执行

你可能想要

  • 在 for 循环中使用警报而不是 console.logs,因此结果直接可见
  • 检查 a 是否为 'null' 并在这种情况下结束游戏(如果您按 'cancel' 或 'esc','a' 将变为 null)
  • 注意,内部 for 循环将一直运行直到完成,即使 win 将同时设置为 true

do{
   for(let i=0; i<=5;i++){
     console.log(i)
     if(i === 3) {      
       win = true
       console.log('win is true, get out of here?!?')
     }
   }
}
while(!win);

console.log('----------------------------- next round');

do{
   for(let i=0; i<=5;i++){
     console.log(i)
     if(i === 3) {      
       win = true
       console.log("win is true, get out with 'break")
       break;
     }
   }
}
while(!win);

描述游戏应该具有的逻辑可能有助于找到可能的编码方式

【讨论】:

    【解决方案2】:

    给你:

    let arr = ['R', 'P', 'S']
    let win = false
    
    function play(arr){
    
      let BotScore = 0;
      let UserScore = 0;
      let round = 1;
      alert('use R as Rock ,P as Paper and S as Scissor')
      while(round<=5){
        let UserChoice = prompt('round_'+round+' Enter choice:')
       
        let BotChoice = Math.floor(Math.random() * 3)
        BotChoice = arr[BotChoice];
        if(UserChoice == 'S' && BotChoice == 'R'){
          BotScore++;
          round++;
          alert('Bot Won')
        }else if(UserChoice == 'S' && BotChoice == 'P'){
          UserScore++;
          round++;
          alert('You Won')
        }else if(UserChoice == 'S' && BotChoice == 'S'){
          alert('Draw')
        }else if(UserChoice == 'R' && BotChoice == 'P'){
          BotScore++;
          round++;
          alert('Bot Won')
        }else if(UserChoice == 'R' && BotChoice == 'S'){
          UserScore++;
          round++;
          alert('You Won')
        }else if(UserChoice == 'R' && BotChoice == 'R'){
          alert('Draw')
        }else if(UserChoice == 'P' && BotChoice == 'R'){
          UserScore++;
          round++;
          alert('You Won')
        }else if(UserChoice == 'P' && BotChoice == 'S'){
          BotScore++;
          round++;
          alert('Bot Won')
        }else if(UserChoice == 'P' && BotChoice == 'P'){
          alert('Draw')
        }
      }
        
        let Result = UserScore > BotScore ? 'You Won' : 'Bot Won'
        alert('Your Score : '+UserScore+' Bot Score : '+BotScore+'['+Result+']')
        
        
      }
    
     
    
    
    play(arr)
    

    【讨论】:

    • 好吧,我想这涵盖了我所有进一步的疑问(如果有的话)。非常感谢!
    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 2013-03-24
    • 2018-05-24
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多