繁体   English   中英

Javascript老虎机如何使用if and else if

[英]Javascript slot machine how to use if and else if

我的代码应该创建一个老虎机,该老虎机用六个符号( Cherry, Bell, Lemon, Orange, Star, Skull )创建一个列表。

  • 使用用户的初始信用额创建一个变量(£1.00)。
  • 有了它,用户就可以从信用中扣除20便士。
  • 从符号列表中生成带有3个随机选择的列表。
  • 打印出选择,以便用户知道结果。
  • 如果用户为铃铛或骷髅以外的任何符号获得三种,则他们的信用增加50便士。
  • 如果用户收到三铃,他们的信用额将增加£1.00。
  • 如果用户得到两个头骨,他们将损失1.00英镑的信用额。
  • 如果用户获得三个头骨,则该用户将失去所有剩余的信用额度。

到目前为止,这是我的代码:

<!DOCTYPE html>
<html>
<body>

<h1>Fruit Machine</h1>

<button type="button" onclick="randomSlot()"> Click to generate slot values.</button>

<script>
var slots = ["Cherry", "Lemon", "Bell", "Orange", "Star", "Skull"];
var credit=100

function randomSlot(){
    var one=slots[Math.floor(Math.random()*slots.length)];
    var two=slots[Math.floor(Math.random()*slots.length)];
    var three=slots[Math.floor(Math.random()*slots.length)];
        document.getElementById("output").innerHTML=one+" "+ two +" "+ three
        credit=credit-20
        document.getElementById("credOutput").innerHTML="Credit:" + credit

        if (one==two && one==three && one!="Skull" && one!="Bell") {
            credit=credit+50;
    }
        else if (one==two && one==three && one="Skull"){
            credit=credit-credit;
    }
        else if (one==two||one==three||two=three && one=="Skull"||two=="Skull"){
            credit=credit-100;
    }
        else if (one==two && one==three && one="Bell"){
            credit=credit+100;
    }
        else {
            credit=credit;
    }
}   



</script>
<p id="output"></p>
<p id="credOutput"></p>

</body>
</html> 

 (one==two && one==three && one="Bell"){
            credit=credit+100;
    }
        else {
            credit=credit;
    }
}   



</script>
<p id="output"></p>
<p id="credOutput"></p>

</body>
</html> 

该错误发生在if语句中的某处,但我不确定如何解决。 任何帮助将不胜感激!

&&优先级高于|| ,所以这条线

   else if (one==two||one==three||two==three && one=="Skull"||two=="Skull"){

可能是错误的。 您需要括号以根据需要对操作进行分组。

   else if ((one==two||one==three||two==three) && (one=="Skull"||two=="Skull")){

您的代码正在被处理,就像您编写的一样:

   else if (one==two || one==three || (two==three && one=="Skull") || two=="Skull"){

通常,每当您对不同的运算符使用复杂的表达式时,最好使用自由括号。

在比较中使用=而不是== ,您还有几种错别字:

    else if (one==two && one==three && one="Skull"){
                                          ^
    else if (one==two && one==three && one="Bell"){
                                          ^

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM