I know it's something I'm doing wrong, but I can't seem to figure out what it is. The console reads the text you input and replies to what you have sent with a message set specifically for your input message. When I input "hello", it replied with "Hello! How are you? (Answers = good, bad)", but when I go to enter either "yes" or "no", the console closes without submitting the message I've specified. Can anybody help me out?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3 {
  class TestClass1 {
    static void Main() {
      String UserInput = Console.ReadLine();
      String PrevAnswer = null;

      switch (PrevAnswer) {
        case null: {
          switch (UserInput) {
            case "hello": {
              Console.WriteLine("Hello! How are you? (Answers = good, bad)");
              break;
            }

            case "goodbye": {
              Environment.Exit(0);
              break;
            }
          }
          break;
        }

        case "hello": {
          switch (UserInput) {
            case "good": {
              Console.WriteLine("That's good to hear");
              Console.ReadLine();
              break;
            }

            case "bad": {
              Console.WriteLine("Oh, that's sad to hear :(");
              break;
            }
          }
          break;
        }
      }
      Console.ReadLine();
    }
  }
}

Recommended Answers

All 3 Replies

At first glance it looks like you're missing a loop. As it is the code only runs through once. Here's a simple way to use a loop:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
    class TestClass1
    {
        static void Main()
        {
            bool Continue = true;
            string UserInput = "";
            String PrevAnswer = null;
            while (Continue)
            {
                switch (PrevAnswer)
                {
                    case null:
                        {
                            UserInput = Console.ReadLine();                            
                            switch (UserInput)
                            {
                                case "hello":
                                    {
                                        Console.WriteLine("Hello! How are you? (Answers = good, bad)");
                                        break;
                                    }
                                case "goodbye":
                                    {
                                        Console.WriteLine("Goodbye\nPress any key to exit.");
                                        Continue = false;
                                        break;
                                    }
                            }
                            break;
                        }
                    case "hello":
                        {
                            UserInput = Console.ReadLine();                            
                            switch (UserInput)
                            {
                                case "good":
                                    {
                                        Console.WriteLine("That's good to hear");    
                                        break;
                                    }
                                case "bad":
                                    {
                                        Console.WriteLine("Oh, that's sad to hear :(");
                                        break;
                                    }
                            }
                            break;
                        }
                    default:
                        Console.WriteLine("Goodbye\nPress any key to exit.");
                        Continue = false;
                        break;
                }
                PrevAnswer = UserInput;
            }
            Console.ReadKey();
        }
    }
}
commented: Thanks for this mate. +0

You're welcome. If this is working for you, please mark this solved. Thanks

how to make a game of hangman on C# console application, static void main()??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.