为什么我的石头剪刀程序不起作用?

时间:2017-09-26 01:56:57

标签: javascript

每次我运行这个程序时,即使我在提示中放入摇滚或纸张,也会弹出一个警告,说明剪刀与x"。它应该说"摇滚与x"如果我放入摇滚乐或纸张与x"如果我放入纸张,但由于某种原因它总是去剪刀,除非我放入的值与计算机生成的值相同。

function setup() {

    alert("Welcome to Rock Paper Scissors!");
}

function draw() {

var opponentChoice = Math.random(); //Setup for game
var yourChoice = prompt("Rock, paper or scissors?");
if (opponentChoice <0.34){
    opponentChoice = "rock";
}else if(opponentChoice <=0.67){
    opponentChoice = "paper";
}
else{
    opponentChoice = "scissors";
} //End of setup for game

function compare (firstOption,secondOption){ //Game logic
    if(firstOption===secondOption){ //Tie if both are the same
        alert(firstOption + " vs. " + secondOption);
        alert("It was a tie!");
    }
    else if(firstOption==="rock"){ //If user types rock
        alert("Rock vs. " + secondOption);
        if(secondOption==="scissors"){ //If computer generates scissors
            alert( "Rock wins");
        }
        else {  //If computer generates paper
            alert( "Paper wins");
        }
    }
    else if(firstOption==="scissors"){ //If user types scissors
        alert("Scissors vs. " + secondOption);
        if(secondOption==="rock"){ //If computer generates rock
            alert( "Rock wins!");
        }
        else { //If computer generates paper
            alert( "Scissors wins");
        }
    } 
    else if(firstOption==="paper"){ //If user types paper
        alert("Paper vs. " + secondOption);
        if(secondOption==="rock"){ //If computer generates rock
            alert( "Paper wins");
        }
        else { //If computer generates scissors
            alert( "Scissors wins");
        }
    }
    else{
        alert("Please choose one of the options in lower case");
    }
};
compare(yourChoice,opponentChoice); //Function is called here

}

1 个答案:

答案 0 :(得分:0)

"use strict";

function draw() {

    let opponentChoice = Math.random(); //Setup for game
    let yourChoice = prompt("Rock, paper or scissors?");
    if (opponentChoice <0.34){
        opponentChoice = "rock";
    }else if(opponentChoice <=0.67){
        opponentChoice = "paper";
    }
    else{
        opponentChoice = "scissors";
    } //End of setup for game

    function compare (firstOption,secondOption){ //Game logic
        if(firstOption===secondOption){ //Tie if both are the same
            alert(firstOption + " vs. " + secondOption);
            alert("It was a tie!");
        }
        else if(firstOption==="rock"){ //If user types rock
            alert("Rock vs. " + secondOption);
            if(secondOption==="scissors"){ //If computer generates scissors
                alert( "Rock wins");
            }
            else {  //If computer generates paper
                alert( "Paper wins");
            }
        }
        else if(firstOption==="scissors"){ //If user types scissors
            alert("Scissors vs. " + secondOption);
            if(secondOption==="rock"){ //If computer generates rock
                alert( "Rock wins!");
            }
            else { //If computer generates paper
                alert( "Scissors wins");
            }
        }
        else if(firstOption==="paper"){ //If user types paper
            alert("Paper vs. " + secondOption);
            if(secondOption==="rock"){ //If computer generates rock
                alert( "Paper wins");
            }
            else { //If computer generates scissors
                alert( "Scissors wins");
            }
        }
        else{
            alert("Please choose one of the options in lower case");
        }
    }

    compare(yourChoice,opponentChoice); //Function is called here

}

draw();

看起来效果很好