0

好的,所以我正在使用 Javascript 使用确认函数、提示符和 if - else 语句创建一个小型业余游戏。但是,在我的代码中,您可以在这里找到:http: //jsfiddle.net/ShiiFtyyMendoza/yFzxj/15/

 function codeAddress() {
     if (prompt('Have you played this game?') === 'no') {
         confirm('This game is like Zork!');
         prompt('Do you need a refresher?');
         if (prompt('Do you need a refresher?') === 'yes'); {
             confirm('All it is, is that you are given a scenario, in which you are also given actions. These actions lead you through a certain path and ending. Pick carefully!');
         }
     } else {
         confirm('Lets play then!');
     }
     //beginning of game
     confirm('Dazed and a little groggy, you realize that you are trapped on an island after a crash landing. Along with other children, you know itd be a best idea to go and keep everybody calm.');
     //stay calm.
     prompt('What are you planning to do? 1. Stay calm and keep everybody in check. or 2. Panic.');
     if (prompt('What are you planning to do? 1.Stay calm and keep everybody in check. or 2. Pacnic') === '1.'); { //this ends after line 19
         confirm('That seems like a great idea. You go around the island, doing your best to convince others to stay calm and keep their composure.');
         prompt('In the attempt to stay calm, you know that youve read about some situation like this - it was in Lord of the Flies, yes! That was it! With Piggy on your side as you walk the beach, you know you had to get an item to get peoples attention. What would you do? 1. Pick up a conch laying on the beach, or 2. Take Piggys glasses');
         if (prompt('In the attempt to stay calm, you know that youve read about some situation like this -  it was in Lord of the Flies, yes! That was it! With Piggy on your side as you walk the beach, you know you had to get an item to get peoples attention. What would you do? 1. Pick up a conch laying on the beach, or 2. Take Piggys glasses') === '1.'); {
             confirm('Yeah, that was probably the best idea. With the conch you could blow into it and end up being able to gather attention and create a leadership.');
         }
     } else {
         {
             confirm('That works too! The sunny day lets you focus the concentrated light onto wood and create a fire.');
             confirm('Soonly, as you wait, you see a helicopter from the horizon come and land. Oh what a miracle! Youve been saved!');
             confirm('Or so you thought');
             confirm('30 minutes into the flight, a dog fight occurs by your position, shooting your helicopter out of the sky, landing you onto another island, this time even farther than before.');
             confirm('Game over.');
         }
     } else {
         {
             confirm('Ahh... this isnt so good. Your commotion had instigated chaos and getting others to follow in your stead would be extremely challenging.');
             confirm('In the choice of panicing, every attempt to gather every or even anybody to keep composure, the group of boys fail to make ends meet. Slowly, as each day passes, you grow weak, only for you to realize that the choices that you had made was a catalyst of failure - causing death for others, and soon enough, you.');
         }
     }
 }
 window.onload = codeAddress;

由于缺少分号,我在第 21 行和第 29 行出现错误。我究竟做错了什么?我真的不知道JS的同谋,所以我很迷茫!任何帮助表示赞赏!


更新
基于 GitaarLAB 的回答,我创建了以下代码:http: //jsfiddle.net/ShiiFtyyMendoza/yFzxj/24/

function codeAddress() {
var answer;
answer = prompt('Have you played this game?');
if (answer === 'no') {
    confirm('This game is like Zork!');
    answer = prompt('Do you need a refresher?')
    if (answer === 'yes') {
        confirm('All it is, is that you are given a scenario, ' + 'in which you are also given actions. ' + 'These actions lead you through a certain path ' + 'and ending. Pick carefully!');
    }
} else {
    {
        confirm('Lets play then!');
    }
}
//beginning of game
confirm('Dazed and a little groggy, you realize that you are ' + 'trapped on an island after a crash landing. \n' + 'Along with other children, you know it\'d be a best ' + 'idea to go and keep everybody calm.');
answer = prompt('What are you planning to do? \n' + '1. Stay calm and keep everybody in check, or \n' + '2. Panic.');
if (answer === '1') {
    {
        { //stay calm
            confirm('That seems like a great idea. \n' + 'You go around the island, doing your best to ' + 'convince others to stay calm and keep their composure.');
            answer = prompt('In the attempt to stay calm, you know that ' + 'youve read about some situation like this - ' + 'it was in Lord of the Flies, yes! That was it! \n' + 'With Piggy on your side as you walk the beach, ' + 'you know you had to get an item to get peoples ' + 'attention. \n\n' + 'What would you do? \n' + '1. Pick up a conch laying on the beach, or \n' + '2. Take Piggys glasses');
        }
        if (answer === '1') {
            {
                {
                    { //pick conch
                        confirm('Yeah, that was probably the best idea. \n' + 'With the conch you could blow into it and end ' + 'up being able to gather attention and create ' + 'a leadership.');
                        answer = prompt('With a tool to help gather attention,' + ' itd be a shame if you didnt put it to good use. \n' + ' Would you  1. try to preserve civilization on the island? \n' + ' or 2. go primitive, in an attempt to escape quickly');
                        if (answer === '1') { //establish leadership - to follower else
                            confirm('With the help of others,' + ' you were able to create multiple shelters' + '. By doing so, you were able to increase your chance of surviving.')
                            answer = prompt('With the comfort of having shelter, you are led into a disagreement with Jack.' + ' He, himself creates a separate group, weakining the once joint operation.' + 'you have a choice to fight back, in an attempt to be able to all go home together as one. \n' + 'What is your decision? 1. Stay calm and sleep in your newly made shelter \n' + 'or \n' + '2. Plan your escape with the others.');
                            if (answer === '1') { //sleep calmly
                                confirm('After your little disagreement, you decided to stay calm, and sleep instead of rising up and fighting.' + ' But as soon as you come out of your shelter, you realize that something is oh so very wrong.' + ' Nobody is here! Everybody else had been eloquently convinced by Jack to leave your intention for his.' + ' That worthless hunting group. He put the human primitive nature in front of priorities!');
                                answer = prompt('You do have a choice though. /n' + '1. Would you go lone wolf and work hard to save yourself? \n' + '2. or would you instead look for them? In the hopes of joining their league.');
                                if (answer === '2') //look for them
                                {
                                    {
                                        confirm('With the decision to walk around and try to find the group,' + ' you hear a chant - the hunting chant.' + ' They were close by...' + ' As you closed up, however, you were attacked by Roger and Jack.' + ' How weird. You dont remember any reason for hostility, until Sam and Eric came by.' + ' Hey! OVER HERE! said Sam and Eric \n' + ' As you came closer, their voices became fainter' + ' Run, they said. That was all they said.');
                                        confirm('You didnt think so at first, but the twins were right.' + ' As soon as you started to run, you were spotted by Jack.' + ' With a sadistic sense, he lit the forest they had all once occupied, on fire.');
                                        confirm('Closer and closer, you ran towards the shore,' + ' but it was a trap!' + ' There was no way out, they had lit the forest on fire to drag you out.');
                                        confirm('Life seems to flash before you, until you realize that a ship started to aproach.' + ' Out came the navy officer, and you were finally saved.');
                                        confirm('Good game!');
                                    }
                                } else { //fire signal - from if followers are gone
                                    confirm('You try to go lone wolf,' + ' in order to, you had created different signals to be seen.' + ' One, was a drawn message on the shore.');
                                    confirm('Another attempt was to set fire on to the forest.');
                                    confirm('This would have worked, however fights had not occured by your position, and you were never discovered.');
                                    confirm('Without any materials to create signals, and the death of many animals and other sources of food,' + ' you slowly perished.');
                                    confirm('Game over');
                                }
                            }
                        } else {
                            { //raft from followers, if from establish leadership
                                confirm('You in the end decide to leave it all, and to do so, you desperately thought of an option to use as an escape.')
                                confirm('With the others boys, you decided a plan.')
                                confirm('Going through the island, looking for stable pieces of wood, you decided to build a raft.')
                                confirm('What a great idea! Youll be able to go home!')
                                confirm('At least you hoped so.')
                                confirm('That same day, as you hopelessly rowed away from the island, a storm hits.')
                                confirm('With your lack of ability to swim for long periods of time, you fatigue and slowly drown to your death.')
                                confirm('It would have probably been better to stay with the other boys at the island.')
                                confirm('Game over.')
                            }
                        }
                    } else { //glasses
                        confirm('That works too! The sunny day lets you focus ' + 'the concentrated light onto wood and create a fire.');
                        confirm('Soonly, as you wait, you see a helicopter from ' + 'the horizon come and land. \n' + 'Oh what a miracle! Youve been saved!');
                        confirm('Or so you thought');
                        confirm('30 minutes into the flight, a dog fight occurs ' + 'by your position, shooting your helicopter out ' + 'of the sky, landing you onto another island, ' + 'this time even farther than before.');
                        confirm('Game over.');
                    }
                }
            }
        }
    } else {
        { //panic
            confirm('Ahh... this isnt so good. \n' + 'Your commotion had instigated chaos and getting ' + 'others to follow in your stead would be extremely ' + 'challenging.');
            confirm('In the choice of panicing, every attempt to gather ' + 'every or even anybody to keep composure, the group ' + 'of boys fail to make ends meet. \n' + 'Slowly, as each day passes, you grow weak, only ' + 'for you to realize that the choices that you had ' + 'made was a catalyst of failure - causing death for ' + 'others, and soon enough, you.');
        }
    }
}
window.onload = function () {
codeAddress();
};

你怎么看?

4

2 回答 2

3

例如:

if (prompt('What are you planning to do? 1.Stay calm and keep everybody in check. or 2. Pacnic') === '1.');

简化版:

if (condition);

if 语句末尾没有分号。PS:你在几行之后犯了同样的错误。

于 2013-05-01T01:49:39.560 回答
0

在您的代码中有很多错误。

除了弗朗西斯科·阿方索在他的回答中指出的一切,我想补充以下几点。

您的最后一行:window.onload = codeAddress;
在您的小提琴中,您已经选择了“onload”选项(它已经自动包装了您的 javascript 代码)。

在其他情况下,您有双重障碍{ statements here }

prompt提出了一个问题,然后在你的if陈述中再次提示了同样的问题。

现在,如果我清理了一些格式和所有错误,并尝试尽可能接近您的代码,结果可能如下所示:

function codeAddress(){
    var answer;
    answer = prompt('Have you played this game?');
    if ( answer === 'no'){
        confirm('This game is like Zork!');
        answer = prompt('Do you need a refresher?')
        if (answer === 'yes'){
            confirm('All it is, is that you are given a scenario, '
                    +'in which you are also given actions. '
                    +'These actions lead you through a certain path '
                    +'and ending. Pick carefully!');
        } else {
        confirm('Lets play then!');
        }
    }

    //beginning of game
    confirm('Dazed and a little groggy, you realize that you are '
            +'trapped on an island after a crash landing. \n'
            +'Along with other children, you know it\'d be a best '
            +'idea to go and keep everybody calm.');
    answer = prompt('What are you planning to do? \n'
                   +'1. Stay calm and keep everybody in check, or \n'
                   +'2. Panic.');
    if ( answer === '1'){  //stay calm
        confirm('That seems like a great idea. \n'
                +'You go around the island, doing your best to '
                +'convince others to stay calm and keep their composure.');
        answer = prompt('In the attempt to stay calm, you know that '
                        +'youve read about some situation like this - '
                        +'it was in Lord of the Flies, yes! That was it! \n'
                        +'With Piggy on your side as you walk the beach, '
                        +'you know you had to get an item to get peoples '
                        +'attention. \n\n'
                        +'What would you do? \n'
                        +'1. Pick up a conch laying on the beach, or \n'
                        +'2. Take Piggys glasses');
        if ( answer === '1') {
            confirm('Yeah, that was probably the best idea. \n'
                    +'With the conch you could blow into it and end '
                    +'up being able to gather attention and create '
                    +'a leadership.');
        } else {
            confirm('That works too! The sunny day lets you focus '
                    +'the concentrated light onto wood and create a fire.');
            confirm('Soonly, as you wait, you see a helicopter from '
                    +'the horizon come and land. \n'
                    +'Oh what a miracle! Youve been saved!');
            confirm('Or so you thought');
            confirm('30 minutes into the flight, a dog fight occurs '
                    +'by your position, shooting your helicopter out '
                    +'of the sky, landing you onto another island, '
                    +'this time even farther than before.');
            confirm('Game over.');
        }
    } else {  //panic
        confirm('Ahh... this isnt so good. \n'
                +'Your commotion had instigated chaos and getting '
                +'others to follow in your stead would be extremely '
                +'challenging.');
        confirm('In the choice of panicing, every attempt to gather '
                +'every or even anybody to keep composure, the group '
                +'of boys fail to make ends meet. \n'
                +'Slowly, as each day passes, you grow weak, only '
                +'for you to realize that the choices that you had '
                +'made was a catalyst of failure - causing death for '
                +'others, and soon enough, you.');
    }
}

window.onload = function(){ codeAddress(); };

在这里工作JSFiddle

其中一项更改是用户在提示框中键入的字符串存储在变量answer中(本地范围为 function codeAddress)。
还有一些关于如何使代码更具可读性以及如何在消息/提示框中换行的提示。

然而,最需要注意的是,如果您查看您或我的代码,您会发现故事情节的嵌套和缩进将成为地狱!
所以你应该开始把事情分解成故事情节的较小部分。
此外,您还需要一个包含所有文本的数组,因此您无需在逻辑中键入所有文本(并且可以稍后翻译您的游戏)。

希望这可以帮助!

于 2013-05-01T03:47:55.980 回答