You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript冒险游戏输入校验问题:输入N未触发失败流程

自定义冒险游戏代码问题排查

我正在制作一款自定义冒险故事游戏,需求为校验用户输入是否为“y”或“n”,若输入无效则重新提问。当前无效输入的重询逻辑正常,但用户输入“n”时,未将变量choice设为false并执行失败提示与重启询问逻辑,反而继续执行for循环;同时怀疑restartGame变量的更新逻辑也存在问题。这是我高中课堂外首次独立编写的正式代码,盼获帮助。

原代码

let food = null;

let storyParts = ["You wake up in a dark hole and an alien approaches you. His name is Tony.",
"You board Tony's ship and you see a goblin in the corner. Tony tells you it doesn't bite.",
"The goblin is visibly shaken and bites off one of your fingers.", 
"Tony feels bad and offers you food.", 
"You eat the " + food]

let storyChoices = ["Do you board his ship? (Yes / No) ", 
"Do you pet the goblin? (Yes / No) ", 
"Spin for food? (Yes / No) ",
"Try again? (Yes / No) "]

let failStatements = ["Tony is disappointed in you. He was your father the whole time. Game over.", 
"Tony is upset. He asks you to leave. Game over."]

let foodOptions = ["pizza", "cupcake", "goblin soup", "finger", "pasta"]

function userInput(line) {
    while (1) {
        let answer = readLine(line);
        if (answer == "y") {
            break;
            return true;
        } else if (answer == "n") {
            break;
            return false;
        } else {
            println("Please type 'y' for yes and 'n' for no.");
        }
    }
}

for (let i = 0, len = storyParts.length; i < len; i++) {
    println(storyParts[i]);
    let choice = null;
    if (i == 0) {
        choice = userInput(storyChoices[0]);
    } else if (i == 1) {
        choice = userInput(storyChoices[1]);
    } else if (i == 4) {
        choice = userInput(storyChoices[2]);
    } else {
        
    }
    if (choice == false) {
        let restartGame = userInput(storyChoices[3]);
        if (restartGame == true) {
            i = -1;
            continue;
        } else if (restartGame == false) {
            i = len;
            break;
        } else {

        }
    }
}

运行输出

You wake up in a dark hole and an alien approaches you. His name is Tony.
Do you board his ship? (Yes / No) n
You board Tony's ship and you see a goblin in the corner. Tony tells you it doesn't bite.
Do you pet the goblin? (Yes / No) y
The goblin is visibly shaken and bites off one of your fingers.
Tony feels bad and offers you food.
You eat the null
Spin for food? (Yes / No) e
Please type 'y' for yes and 'n' for no.
Spin for food? (Yes / No) e
Please type 'y' for yes and 'n' for no.
Spin for food? (Yes / No) n

问题原因及修复方法

1. userInput函数返回值失效

函数里break写在return前面,导致return永远不会执行,choice始终为null,无法触发失败逻辑。
修复:删除多余的break,直接返回结果:

function userInput(line) {
    while (1) {
        let answer = readLine(line);
        if (answer == "y") {
            return true;
        } else if (answer == "n") {
            return false;
        } else {
            println("Please type 'y' for yes and 'n' for no.");
        }
    }
}

2. 失败提示未触发

当前代码未在用户输入n时调用failStatements输出失败信息,需补充对应逻辑:

if (choice == false) {
    // 根据步骤输出对应失败语句
    if (i == 0) {
        println(failStatements[0]);
    } else if (i == 1) {
        println(failStatements[1]);
    }
    // 保留原重启逻辑
    let restartGame = userInput(storyChoices[3]);
    if (restartGame == true) {
        i = -1;
        continue;
    } else if (restartGame == false) {
        i = len;
        break;
    }
}

3. 食物变量未正确赋值

第4个故事片段里food初始为null,导致输出You eat the null,需在用户选择“Spin for food”时随机赋值:

else if (i == 4) {
    choice = userInput(storyChoices[2]);
    if (choice) {
        // 随机选一种食物
        food = foodOptions[Math.floor(Math.random() * foodOptions.length)];
        // 更新故事片段内容
        storyParts[4] = "You eat the " + food;
    }
}

4. 重启逻辑优化

重启时需重置food变量,避免复用上次游戏的食物值:

if (restartGame == true) {
    food = null;
    storyParts[4] = "You eat the " + food;
    i = -1;
    continue;
}

内容的提问来源于stack exchange,提问作者Anthony Wright

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 20:44:50