我当时正在做一个剪刀石头布挑战,当我需要设置一个决定获胜者的函数时,我觉得编写大量包含每个潜在情况的 if 语句根本不切实际。但在我进行了一次小搜索之后,我检查过的所有代码都使用了 if 语句。\xe2\x80\x99t 有没有更好的方法来做到这一点?
\n我将分享下面的代码:
\n //after the user clickes on scissor or paper or rock mymove function runs.\nfunction mymove(choice) {\n let humanchoice = choice.id \n //a while loop that runs till the computer choice is not the same as the user choice\n while (true){\n let pcnum = Math.random();\n console.log(pcnum);\n let pcchoie = 'paper';\n\n \n if(pcnum <= 0.33){\n pcchoie = "paper";\n \n } else if(pcnum <= 0.66){\n pcchoie = "scissor"; \n }else {\n pcchoie = "rock"; \n …Run Code Online (Sandbox Code Playgroud)