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

Java MadLibs游戏开发:如何保存每个输入而非仅保留最后一个

问题

我开发了一款Java版MadLibs游戏,代码会根据原文提示让用户输入名词(noun)或动词(verb),但遍历完成后,输出的改写文本仅使用了最后输入的名词和动词。现需在不允许使用数组的前提下,修改代码使每个输入对应替换原文中相应位置的noun/verb。

以下是我的代码:

import java.util.Scanner;
public class MadLibsProject
{
    public static void story()
    {
        Scanner in = new Scanner(System.in);
        String noun = "";
        String verb = "";
        String story = "The Little Noun And The Old Noun By Shel Silverstein \nSaid the little boy, "+"sometimes I drop my noun. \nSaid the little old man, I verb that too.\nThe little boy whispered, I verb my noun.\n"+"I do too, laughed the old noun. \nSaid the little boy, I often cry. \nThe old "+"man nodded. So do I. \nBut worst of all, verb the noun, "+"it seems grown-ups don’t pay attention to me. \nAnd he felt the warmth of a "+"wrinkled old hand. \nI verb what you verb, said the little old noun.";
        String update ="";
        String noun1 = noun;
        String verb1 = verb;
        for(int i =0;i< story.length()-4;i++)
        {
            String madlib = story.substring(i,i+4);
            if("noun".contains(madlib) || ("Noun".contains(madlib)))
            {
                System.out.println("Please enter a noun");
                noun = in.next();
                
            }
            
            
            else if("verb".contains(madlib))
            {
                System.out.println("Please enter a verb");
                verb = in.next();
                verb1 = verb;
            }
            noun1 = noun;
            verb1 = verb;
            update = store(noun1,verb1);
            
        }
        
        System.out.print(update);
    }
    public static String store(String noun,String verb)
    {   
        // for int i = 0;i < story.length()-4; i++
        // madlib = story.substring(i,i+4)
        // noun.contains mmadlib
        String story = "The Little "+ noun+" And The Old "+noun+" By Shel Silverstein \nSaid the little boy, "+"sometimes I drop my "+noun+". \nSaid the little old man, I "+verb+" that too.\nThe little boy whispered, I "+verb+" my "+noun+".\n"+"I do too, laughed the old "+noun+". \nSaid the little boy, I often cry. \nThe old "+"man nodded. So do I. \nBut worst of all, "+verb+" the "+noun+", "+"it seems grown-ups don’t pay attention to me. \nAnd he felt the warmth of a "+"wrinkled old hand. \nI "+verb+"  what you "+verb+", said the little old "+noun+"";
        return story;
        
    }
    public static void main(String[] args)
    {
        
        story();
        
        
    }
}

当前输入输出示例

输入:

Please enter a noun
noun1
Please enter a noun
noun2
Please enter a noun
noun3
Please enter a verb
verb1
Please enter a verb
verb2
Please enter a noun
noun4
Please enter a noun
noun5
Please enter a verb
verb3
Please enter a noun
noun6
Please enter a verb
verb4
Please enter a verb
verb5
Please enter a noun
noun7

输出:

The Little noun7 And The Old noun7 By Shel Silverstein
Said the little boy, sometimes I drop my noun7.
Said the little old man, I verb5 that too.
The little boy whispered, I verb5 my noun7.
I do too, laughed the old noun7.
Said the little boy, I often cry.
The old man nodded. So do I.
But worst of all, verb5 the noun7, it seems grown-ups don’t pay attention to me.
And he felt the warmth of a wrinkled old hand.
I verb5  what you verb5, said the little old noun7.

解决方案

原代码核心问题是:遍历过程中仅反复覆盖noun和verb变量,最后调用store方法时用最后一次输入的值统一替换所有位置,导致所有名词/动词都变成最后输入的内容。

要解决这个问题,我们可以直接在遍历原文模板的过程中完成实时替换,每遇到一个noun或verb就立即替换成用户输入的内容,同时维护动态更新的结果字符串,完全不需要数组。

修改后的代码如下:

import java.util.Scanner;

public class MadLibsProject {
    public static void story() {
        Scanner in = new Scanner(System.in);
        // 原始故事模板,保留大小写区分的Noun和小写noun
        String storyTemplate = "The Little Noun And The Old Noun By Shel Silverstein \n" +
                "Said the little boy, sometimes I drop my noun. \n" +
                "Said the little old man, I verb that too.\n" +
                "The little boy whispered, I verb my noun.\n" +
                "I do too, laughed the old noun. \n" +
                "Said the little boy, I often cry. \n" +
                "The old man nodded. So do I. \n" +
                "But worst of all, verb the noun, it seems grown-ups don’t pay attention to me. \n" +
                "And he felt the warmth of a wrinkled old hand. \n" +
                "I verb what you verb, said the little old noun.";
        
        // 使用StringBuilder动态修改字符串,效率更高
        StringBuilder result = new StringBuilder(storyTemplate);
        int i = 0;
        
        while (i < result.length()) {
            // 匹配首字母大写的Noun
            if (i + 4 <= result.length() && result.substring(i, i + 4).equals("Noun")) {
                System.out.println("Please enter a noun");
                String inputNoun = in.next();
                // 替换当前位置的Noun为用户输入
                result.replace(i, i + 4, inputNoun);
                // 跳过新输入内容的长度,避免重复匹配
                i += inputNoun.length();
            } 
            // 匹配小写的noun
            else if (i + 4 <= result.length() && result.substring(i, i + 4).equals("noun")) {
                System.out.println("Please enter a noun");
                String inputNoun = in.next();
                result.replace(i, i + 4, inputNoun);
                i += inputNoun.length();
            } 
            // 匹配小写的verb
            else if (i + 4 <= result.length() && result.substring(i, i + 4).equals("verb")) {
                System.out.println("Please enter a verb");
                String inputVerb = in.next();
                result.replace(i, i + 4, inputVerb);
                i += inputVerb.length();
            } else {
                // 不是目标词,移动到下一个字符
                i++;
            }
        }
        
        // 输出最终结果
        System.out.println(result.toString());
        in.close();
    }

    public static void main(String[] args) {
        story();
    }
}

修改说明

  1. 用StringBuilder动态构建结果,比字符串拼接更高效,且支持实时替换操作。
  2. 遍历过程中逐个检查字符子串,匹配到目标词后立即让用户输入并替换当前位置,确保每个输入对应正确的位置。
  3. 替换完成后将索引移动到新内容的末尾,避免重复匹配已替换的内容。
  4. 移除了原有的store方法,因为现在是实时生成结果,不需要最后统一替换。

修改后,每个用户输入的名词/动词都会精准替换到原文对应的位置,不会再出现全部使用最后一次输入的情况。

内容的提问来源于stack exchange,提问作者Abdelrhaman Nianaa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 06:17:03