【问题标题】:I need to return a variable from my game我需要从我的游戏中返回一个变量
【发布时间】:2014-05-25 22:26:24
【问题描述】:

由于我现在有游戏,它会根据用户的选择循环多次,我需要将玩过的数量除以获胜的数量以获得获胜百分比。我不知道如何去做,所以任何帮助表示赞赏。提前致谢。

 function rockPaperScissors(){
            function game(wantToPlay){
                if (wantToPlay=="Yes"||wantToPlay=="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    //gets a random number
                    var computerChoice = Math.random();

                    //assigns that random number to be either rock, paper or scissors based on the number
                    if (computerChoice < .34) {
                        computerChoice = "rock";}
                    else if(computerChoice <= .66) {
                        computerChoice = "paper";}
                    else {
                        computerChoice = "scissors";
                    }
                    console.log("computerChoice: " + computerChoice);
                    console.log("userChoice: " + userChoice);

                    //compares the computer choice to the user choice and then prints out the outcome on the page.
                    function compare(choice1,choice2){
                        console.log(choice1 + "<-----User");
                        console.log(choice2 + "<-----Computer");
                        if (choice1==choice2){
                            alert("It was a tie!");
                            game("yes");
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";

                            }
                            /**else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Paper smothers rock.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";

                            }
                           /** else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Scissors cut paper.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
                            }
                           /** else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else{
                            alert("Very funny. Read the help menu and do it right.");
                            game("yes");
                        }

                    };
                    compare(userChoice,computerChoice);
                }

                //this only runs if the user does not type yes
                else{
                    document.getElementById("messages").innerHTML="Well alrighty then.";
                    document.getElementById("loss").innerHTML="";
                    document.getElementById("win").innerHTML="";
                }
            }
            //promts the start of the game and loops x amount of times based on user input.
            var start = prompt ("Do you want to play?","Yes");
            var i = prompt("How many times do you want to play?", 5);
            for (n = 0; n < i; n ++){
            game(start);
        }}

【问题讨论】:

  • Protip:将你的元素存储为变量,并停止滥用innerHTMLD:
  • 你必须计算输赢。似乎您可以通过在 if 语句中增加几个变量轻松地做到这一点,然后将其除以所玩游戏的数量以获得百分比......
  • cale_b 是否可以在 if 语句中工作,游戏是循环的,所以每次游戏循环时胜负变量不会重置为其分配的值吗?
  • 我想最简单的方法就是尝试一下。
  • 这不起作用,它不保存递增的值,只是在每次游戏循环时重置它

标签: javascript variables loops methods


【解决方案1】:

声明变量,像这样(在&lt;body onload="initVars()"&gt; 并创建函数initVars()

var wins=0;
var plays=0;

当玩家获胜时,你会使用 wins++plays++,当玩家失败时 - 仅 plays++
当游戏结束时,你会做wins/plays

【讨论】:

    猜你喜欢
    • 2017-10-12
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2021-05-15
    • 2019-12-27
    相关资源
    最近更新 更多