Javascript/HTML老虎机游戏:不会运行Javascript
我一直在做这个吃角子老虎机的HTML/Javascript游戏。我知道它的数学方面是有效的,因为我基本上是从我制作的python版本中提取出来的,并尝试将其格式化为Javascript。HTML内容显示出来,但实际的程序都无法运行。我尝试在内部和外部链接Javascript,但两者都不起作用Javascript/HTML老虎机游戏:不会运行Javascript,javascript,html,Javascript,Html,我一直在做这个吃角子老虎机的HTML/Javascript游戏。我知道它的数学方面是有效的,因为我基本上是从我制作的python版本中提取出来的,并尝试将其格式化为Javascript。HTML内容显示出来,但实际的程序都无法运行。我尝试在内部和外部链接Javascript,但两者都不起作用 <!DOCTYPE html> <html> <head> <title>Big Cat's Slot Machine!</title>
<!DOCTYPE html>
<html>
<head>
<title>Big Cat's Slot Machine!</title>
</head>
<body bgcolor= "#81D2F7">
<h2>I'll give you 100 monies to begin the game.</h2>
<h2>To win, you must get one of the following combinations:</h2>
<h2>Three cheetahs will pay $50 * bet</h2>
<h2>Three pumas will pay $30 * bet</h2>
<h2>Three tigers will pay $20 * bet</h2>
<h2>Three lions will pay $10 * bet</h2>
<h2>Three leopards will pay $4 * bet</h2>
<h2>Two leopards will pay $2 * bet</h2>
<h2>One leopard will pay $1 * bet</h2>
<h2>If you hit a balance of zero, the game will end.</h2>
<h2>Good luck, matey.</h2>
<script type="text/javascript"src="myscripts.js"/>
</body>
</html>
我猜这可能很简单,比如缺少一个分号或括号什么的,你知道吗?在关闭while的括号之后,如果你先放了一个分号,我想这是错误的。
而条件(money>0)永远不会是真的,因为你在做money=0;在此之前,控件将永远不会进入循环。首先,Javascript不会使用import关键字导入库。import是一个Python关键字。因此,您不需要Javascript文件顶部的
import Random
行
其次,您的随机数生成器调用应如下所示:
var a=NumberChanger(Math.floor(Math.random()*5)+1)代码>
…和其他错误
您可以在这里找到工作文件:您如何提供myscripts.js
?Web浏览器希望获得他们的资源vai URIsrc=“myscripts.js
不是有效的URI。您缺少一些引号,请查看语法突出显示。@JamesMillsmyscripts.js
是一个非常好的URL,这意味着js文件与HTML文件位于同一文件夹中。@Barmar它实际上不是这样工作的;但是无论如何:)您在Javascript控制台中有任何错误吗?
import random
var money = 0;
var cats = ['tiger', 'lion', 'cheetah', 'puma', 'leopard'];
while (money > 0) {
document.write("You have ", money, " money");
var wager= prompt("Press 0 to exit or any other number to bet that");
if (wager == 0) {
break;
}
else if (wager > 0) {
var a = NumberChanger(Math.random(1,5);)
var b = NumberChanger(Math.random(1,5);)
var c = NumberChanger(Math.random(1,5);)
document.write(a);
document.write(b);
document.write(c);
if (a == 'cheetah' and b == 'cheetah' and c == 'cheetah') {
return money= ((money-wager) + (wager*50));
}
else if (a == 'puma' and b == 'puma' and c == 'puma') {
return money= ((money-wager) + (wager*30);
}
else if (a == 'tiger' and b == 'tiger' and c == 'tiger') {
return money= ((money-wager) + (wager*20));
}
else if (a== 'lion' and b== 'lion' and c== 'lion'){
return money= ((money-wager) + (wager*10));
}
else if (a== 'leopard' and b== 'leopard' and c=='leopard) {
return money= ((money-wager) + (wager*4));
}
else if (a== 'leopard' and b== 'leopard') {
return money= ((money-wager) + (wager*2));
}
else if (a== 'leopard' and c== 'leopard') {
return money= ((money-wager) + (wager*2));
}
else if (b== 'leopard' and c== 'leopard') {
return money= ((money-wager) + (wager*2));
}
else if (a== 'leopard' or b== 'leopard' or c== 'leopard'){
return money= ((money-wager) + (wager*1));
}
else {
return money= ((money-wager));
}
};
};
var NumberChanger = function(Number)
{
if (Number == 1) {
Number= ('tiger');
}
else if (Number == 2) {
Number= ('lion');
}
else if (Number == 3){
Number= ('puma');
}
else if (Number == 4) {
Number= ('leopard');
}
else if (Number == 5){
Number= ('cheetah');
}
return (Number);
};