开发者

How to code C# Mad Libs program with loops / minimum code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 11 hours ago.

Improve this question

I wrote a little Mad Libs program for my daughter to request word types and include the user input words in a story, the typical Mad Libs. But I want to reduce the amount of code. I'm able to request the word inputs with foreach loops (I created an Enum with all wordTypes and used writeLine to ask for each) but I'm not sure how to store each separately and then have them put into the story. Any help would be appreciated!

This is the original code:

namespace MadLibs
{
    class Program   
    {
        static void Main(string[] args)
        {
            Story story = new Story();
            story.StoryInfo();            开发者_运维知识库
        }
    }
    class Story 
    {
        string person1, person2, person3;
        string object1, object2, object3, object4;
        string verb1, verb2, verb3, verb4;
        string adj1, adj2, adj3, adj4;
        string place1, place2, place3;

        public void StoryInfo()
        {

            Console.Write("Enter a person: ");
            person1 = Console.ReadLine();

            Console.Write("Enter another person: ");
            person2 = Console.ReadLine();

            Console.Write("Enter another person: ");
            person3 = Console.ReadLine();

            Console.Write("Enter an object: ");
            object1 = Console.ReadLine();

            Console.Write("Enter another object: ");
            object2 = Console.ReadLine();

            Console.Write("Enter another object: ");
            object3 = Console.ReadLine();
            
            Console.Write("Enter another object: ");
            object4 = Console.ReadLine();

            Console.Write("Enter an adjective: ");
            adj1 = Console.ReadLine();

            Console.Write("Enter another adjective: ");
            adj2 = Console.ReadLine();

            Console.Write("Enter another adjective: ");
            adj3 = Console.ReadLine();
            
            Console.Write("Enter another adjective: ");
            adj4 = Console.ReadLine();

            Console.Write("Enter a place: ");
            place1 = Console.ReadLine();

            Console.Write("Enter another place: ");
            place2 = Console.ReadLine();

            Console.Write("Enter another place: ");
            place3 = Console.ReadLine();

            Console.Write("Enter an action: ");
            verb1 = Console.ReadLine();

            Console.Write("Enter another action: ");
            verb2 = Console.ReadLine();

            Console.Write("Enter another action: ");
            verb3 = Console.ReadLine();
            
            Console.Write("Enter another action: ");
            verb4 = Console.ReadLine();


            Console.WriteLine($"\nOnce there was a {adj1} {object1} named {person1}." +
                $"\nThey loved to {verb1} all day with their {object2} at the {place1}." +
                $"\nBut one day a {adj2} person named {person2} came up to {person1} and said," +
                $"\nLet's go to the {place2}, my friend {person3} is there and is {verb1}ing with {object3}s." +
                $"\nBut on their way there, they saw that there was a {adj3} {place3}" +
                $"\nthat looked like a {adj4} place to {verb4}." +
                $"\nSo {person1} and {person2} stayed at the {place3}, {verb4}ing with {object4}s.");

        }
    
    }
}

Explained in OP, long code, then tried to reduce with enum/foreach loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜