如何使程序恢复原始文本而不重新输入

时间:2015-05-23 22:53:44

标签: javascript

if (playerChoseWeapon === true) {

    var roomGenerator = Math.Random();

    if (roomGenerator > 0.75) {

        var roomGeneratorRoomOne = Math.Random();

        if (roomGeneratorRoomOne > 0.75) {

            var rGenR1One = prompt("You see a dusty room with a door on the other side, what do you want to do? REST, DOOR, or STOP");

            if (rGenR1One === REST && playerHealthPoints < 5) {

                playerHealthPoints++;

                //Make so it goes back to original thing!

            } else if (rGenR1One === STOP) {

                alert("Script stopped, you can restart now.");

                //Maybe add some way to ask if they want to restart and it will let them?!

            } else if (rGenR1One === DOOR) {


            }

代码在那里看起来很丑,但在我的编辑器中看起来好多了。

当你做REST时,基本上有“你看到一个尘土飞扬的房间......”中的代码,它会这样做,它会恢复到同样的“你看到一个尘土飞扬的房间......”。

所以我不需要重新输入数千次。

1 个答案:

答案 0 :(得分:1)

你可以使用do while循环:

if (playerChoseWeapon === true) {
    var roomGenerator = Math.Random();
        if (roomGenerator > 0.75) {
        var roomGeneratorRoomOne = Math.Random();
        if (roomGeneratorRoomOne > 0.75) {
            do{
                var rGenR1One = prompt("You see a dusty room with a door on the other side, what do you want to do? REST, DOOR, or STOP");
            if (rGenR1One === REST && playerHealthPoints < 5) {
                playerHealthPoints++;
                //Make so it goes back to original thing!
            } else if (rGenR1One === STOP) {
                alert("Script stopped, you can restart now.");
                //Maybe add some way to ask if they want to restart and it will let them?!
            } else if (rGenR1One === DOOR) {
            }
        }while(rGenR1One === REST)

或者喜欢这样:

if (roomGeneratorRoomOne > 0.75) {
        var rGenR1One = prompt("You see a dusty room with a door on the other side, what do you want to do? REST, DOOR, or STOP");
        if (rGenR1One === REST && playerHealthPoints < 5) {       
            playerHealthPoints++;
            do{
                var rGenR1One = prompt("You see a dusty room with a door on the other side, what do you want to do? REST, DOOR, or STOP");
                 if (rGenR1One === REST && playerHealthPoints < 5) {
                            playerHealthPoints++;
                 }
            }while(rGenR1One === REST)
        } else if (rGenR1One === STOP) {
            alert("Script stopped, you can restart now.");
            //Maybe add some way to ask if they want to restart and it will let them?!
        } else if (rGenR1One === DOOR) {
        }
}