不确定我是否有语法错误或代码错误

问题描述 投票:0回答:2

我目前正在创建一个骰子游戏,您可以掷两个骰子,然后由 javascript 决定您是输还是赢,与计算机打平。这是简单的代码,但我不确定我是否犯了语法错误或代码本身。

我已经尝试过查看代码,但我对此还很陌生,一些专业的眼睛会很有用。

<!DOCTYPE html>
<html>

<head>
<title> Game of dice </title>
<script> 

var player;
var computer;

function randDice() {
var number = randNum(1,6);  
return number;
}

function rollDice(){


player = randDice();
computer = randDice();


if (player == 1) {
document.getElementById("dieOne").src= "die1.png"
} else if (player == 2) {
document.getElementById("dieOne").src= "die2.png"
} else if (player == 3) {
document.getElementById("dieOne").src= "die3.png"
} else if (player == 4) {
document.getElementById("dieOne").src= "die4.png"                    
} else if (player == 5) {
document.getElementById("dieOne").src= "die5.png"
} else (player == 6) {
document.getElementById("dieOne").src= "die6.png"   

if (computer == 1) {
document.getElementById("dieTwo").src= "die1.png"
} else if (computer == 2) {
document.getElementById("dieTwo").src= "die2.png"
} else if (computer == 3) {
document.getElementById("dieTwo").src= "die3.png"
} else if (computer == 4) {
document.getElementById("dieTwo").src= "die4.png"
} else if (computer == 5) {
document.getElementById("dieTwo").src= "die5.png"
} else (computer == 6) {
document.getElementById("dieTwo").src= "die6.png"
checkWin();
}
function checkWin(){

if (player == computer){
    document.getElementById("winner").innerHTML = "You tied";
} else if (player > computer){
    document.getElementById("winner").innerHTML = "You-won!";
} else (player < computer){
    document.getElementById("winner").innerHTML = "You-lost";
}
}

</script>

</head>

<body>

<h1> Die roll </h1>
<img src="die1.png" id="dieOne"><image>
<img src="die2.png" id="dieTwo"><image>
<br>
<br>
<button onClick="rollDice();">Roll</button>
<br>
<p id="winner"></p>
</body>
</html>
javascript
2个回答
1
投票

您的源代码中存在一些错误:

  • rollDice 函数中缺少 }
  • 你错了 else if 条件
    else (player == 6)
  • 将 script 标签放在 head 中,需要移动到关闭 body 标签。
  • 另请检查 randNum() 方法的参考

您可以按 F12 并查看控制台选项卡以了解第哪一行代码的详细错误消息。

enter image description here

<!DOCTYPE html>
<html>

<head>
<title> Game of dice </title>

</head>

<body>

<h1> Die roll </h1>
<img src="die1.png" id="dieOne"><image>
<img src="die2.png" id="dieTwo"><image>
<br>
<br>
<button onClick="rollDice();">Roll</button>
<br>
<p id="winner"></p>

<script> 

var player;
var computer;

function randDice() {
var number = randNum(1,6);  
return number;
}

function rollDice(){


player = randDice();
computer = randDice();


if (player == 1) {
document.getElementById("dieOne").src= "die1.png"
} else if (player == 2) {
document.getElementById("dieOne").src= "die2.png"
} else if (player == 3) {
document.getElementById("dieOne").src= "die3.png"
} else if (player == 4) {
document.getElementById("dieOne").src= "die4.png"                    
} else if (player == 5) {
document.getElementById("dieOne").src= "die5.png"
} else if(player == 6) {
document.getElementById("dieOne").src= "die6.png"
}

if (computer == 1) {
document.getElementById("dieTwo").src= "die1.png"
} else if (computer == 2) {
document.getElementById("dieTwo").src= "die2.png"
} else if (computer == 3) {
document.getElementById("dieTwo").src= "die3.png"
} else if (computer == 4) {
document.getElementById("dieTwo").src= "die4.png"
} else if (computer == 5) {
document.getElementById("dieTwo").src= "die5.png"
} else if(computer == 6) {
document.getElementById("dieTwo").src= "die6.png"
checkWin();
}

}
function checkWin(){

if (player == computer){
    document.getElementById("winner").innerHTML = "You tied";
} else if (player > computer){
    document.getElementById("winner").innerHTML = "You-won!";
} else if(player < computer){
    document.getElementById("winner").innerHTML = "You-lost";
}
}

</script>

</body>
</html>


0
投票

在这是我的代码之前我已经完成了这个游戏

<html>

<head>

   <script>
    var roll1;

    var roll2;  


       function rollTheDice()
       {  
           roll1 = Math.floor(Math.random() * 6) + 1;

            document.getElementById('imgDice').src = roll1 + ".jpg";

           return roll1;
       }

        function rollTheDice2()
       {  
            roll2 = Math.floor(Math.random() * 6) + 1;

            document.getElementById('imgDice2').src = roll2 + "a.jpg";

           return roll2;
       }


            function compareDice()
       {  
         if (roll1 < roll2 ) {alert("You Loose")
             ;
             } else if (roll1 > roll2 )  {
             alert ("You Win")
               ;
               } else {alert ("You Tie")
                ;
                }
    }

   </script>
</head>
<body>


<input type="button"
   onclick="rollTheDice();"
   value="Get Your Die"/>
<img src="" id="imgDice"/>

    <br>

    <input type="button"
   onclick="rollTheDice2();"
   value="Choose Opponent's Die"/>
<img src="" id="imgDice2"/>
    <br>

    <input type="button"
   onclick="compareDice();"
   value="Who wins?"/>
<img src="" id="imgDice"/>



</body>
</html>


© www.soinside.com 2019 - 2024. All rights reserved.