我的代码应该创建一个老虎机,该老虎机创建一个包含六个符号(Cherry, Bell, Lemon, Orange, Star, Skull
)的列表。
到目前为止,这是我的代码:
<!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语句中的某处,但我不确定如何解决。任何帮助将不胜感激!
答案 0 :(得分:1)
&&
的优先级高于||
,因此此行
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"){
^