0

奇怪的是我看不到任何语法错误。我也是 javascript 新手,但绝不是无能。但是每当我运行我的代码时,我使用的任何浏览器都会在空白网页上打印出我的整个代码,而不执行任何代码。我以前从未遇到过这个问题。由于代码未执行,我无法使用任何浏览器调试工具。

它不像我没有像往常一样正确启动代码

<html>
<body>
<script type="text/javascript">

然后是我的代码,然后关闭标签

奇怪的是,上面的代码甚至被打印在网页上,所以它从一开始就没有被识别为 javascript

以前有人遇到过这个问题吗?我错过了什么吗

<html>
<body>
<script type="text/javascript">

var die1;           // the 3 dice
var die2;
var die3;

var username;   // users name

var bet  = 0;            // variable for the amount the user bets
var input;              // used to check the bet is a number

var credits = 100;          // users overall credits and starting amount
var round = 0;          // game rounds , will have a maximum of 10 rounds


username = window.prompt("Welcome to Triple Roll! Please enter your name");     // prompts                  the user to enter their name
gamestart;                      // calls the first function to start the game!




var rolldice = function()       // puts all 3 dice rolls into a function to be easily called.
{
die1 = Math.round(Math.random()*6);         // random number generators for the 3 dice
die2 = Math.round(Math.random()*6);         // dieX is an integer (round number) in the  range of 1 to 6
die3 = Math.round(Math.random()*6);
}


var gamestart = function()   // Using this function the game can be reset to starting values, The user is also asked if they would like to hear the rules
{
bet = 0;
credits = 100;
round = 0;

if (window.prompt("Hello" + username ", Do you wish to go over the the instructions?   Y/N ")) = "Yes" || "yes" || "y" || "Y"    // if the user says yes then show rules , else run the game
    { 
confirm(" This game has 10 rounds, at the beginning of the game you will start with 100 credits to bet with. For each round of the game you will bet on the outcome of 3 dice rolls each with numbers 1 through 6. continue?");
confirm(" For each round you may bet between 1 and 100 credits regardless of how many credits you actually have, though be warned if you lose and end up with 0 credits then its game over.");
confirm(" The value of all 3 dice will determine whether or not you win or lose credits, but don't worry , you will be able to see how many credits you have at any time");
confirm(" If the 3 die all show a 1 , then you will win 50 times your bet plus the original stake back, If the 3 die all show the same value other than 1 then you win 30 times your bet plus your original stake back");
confirm("If 2 die show the same value, then you win 8 times your bet plus your original bet. If you don't meet any of the requirements but the values of die add up to 15 or more, then you win 2 times your bet plus the original stake.");
confirm("If one of the die is a 6 or all 3 of the die are odd numbers, then you get your bet back and may try again. In all other situations you lose your bet");
    }
else 
    { 
game;   // call the game function 
    }

}

 var game = function()
{ 
input = window.prompt("Round" + round ". You have" + credits "credits. Please enter a bet between 0-100"); // shows the user what round it is, how many credits they have and asks them to make a bet
bet = parseInt(input);          // checks the input is a number

 if (bet != 0-100);         // if the bet isn't between 0-100
    { 
    game;               // restart the function
    }
else                        // else
    { 
    rolldice;           // call the roll dice function
    }

confirm(" The Die rolls are" + die1 + die2 + die3);     // let user know what the dice values are
    winoutcomes;                // call the function that checks to see if the user has won

}

4

2 回答 2

0

您是否在脚本中调用函数?如果调用不带括号的函数,它会在函数内输出脚本。

例子:

假设我们有以下代码:

function printHiThere(name) {
   document.write('Hi there, ' +  name);
}

如果你调用它使用

printHiThere('bob');

那么输出将是:

Hi there, bob

但是如果你在没有参数或括号的情况下调用它,像这样:

printHiThere

然后输出是:

function printHiThere(name) {
   document.write('Hi there, ' +  name);
}

这可能是你的问题吗?

于 2013-10-25T16:01:01.903 回答
0

你的文件名是'xxxxxx.js'

它需要是'xxxxx.html'

而且最好不要在其中也有空间。

所以将'Assignment 01.js'更改为'Assignment01.html'

重命名您的文件。

它会起作用的。

你刚刚发布了一些我看到的代码。它没有任何关闭的 script/body/html 标签

于 2013-10-25T16:17:30.427 回答