user5249863
user5249863

Reputation:

How can i have a case in a switch statement that lets a user input a string to be displayed

So i've got this code. I have it so that when the user inputs a number 1-4 it will display a different saying plus their name at the beginning based on which number they input. What i want to do is make it so that when they input a -1 instead of 1, 2, 3 or 4 it allows them to type in their own saying to be output.

using System;

namespace TryIt  
{    
 class Conversation  
  {
    // Declare constants that define the input range
    private const int MIN = -1;
    private const int MAX = 4; 

    // Declare constants for the colors to be used
    private const ConsoleColor INPUT_COLOR = ConsoleColor.Green;
    private const ConsoleColor PROMPT_COLOR = ConsoleColor.Blue;
    private const ConsoleColor ERROR_COLOR = ConsoleColor.Red;
    private const ConsoleColor MESSAGE_COLOR = ConsoleColor.White;
    private const ConsoleColor BACKGROUND_COLOR = ConsoleColor.DarkGray;

    private String name;

    public Conversation()
    {
        Console.Title = "The Best TryIt Program";
        Console.BackgroundColor = BACKGROUND_COLOR;
        Console.Clear();
    }

    public void go()
    {
        getName();
        int number = getNumber();
        displayMessage(number);

        Console.ForegroundColor = ConsoleColor.Gray;
        Console.WriteLine();
    }

    private void getName()
    {
        Console.ForegroundColor = PROMPT_COLOR;
        Console.Write("Enter your name.   ");
        Console.ForegroundColor = INPUT_COLOR;

        // This will ask what your name is and prompt you to enter it.
        name = Console.ReadLine();
    }

    private int getNumber()
    {
        int input;

        // int input will take your name and display it back to you
        do
        {
            Console.ForegroundColor = PROMPT_COLOR;
            Console.Write("\nEnter a number from 1 to 4.  ");
            Console.ForegroundColor = INPUT_COLOR;

            // This will ask you for a number
            input = Convert.ToInt16(System.Console.ReadLine());

            // If you don't put in a valid number it will tell you
            if (input < MIN || input > MAX)
            {
                Console.ForegroundColor = ERROR_COLOR;
                Console.WriteLine("Invalid Number!");
            }
        } while (input < MIN || input > MAX);

        return input;
    }

    private void displayMessage(int choice)
    {
        String phrase = " ";

        // Declares the variable (a string) for the output messages.
        switch (choice)
        {
            case 1: phrase = " is a genius!"; break;
            case 2: phrase = " is amazing!"; break;
            case 3: phrase = " came to chew bubblegum" + 
                    ", kick butt " + "and is all out of gum."; break;
            case 4: phrase = " is a rockstar!"; break;
        }


        Console.WriteLine();
        Console.ForegroundColor = MESSAGE_COLOR;

        // This will display the message you selected 10 times
        for (int counter = 0; counter < 10; counter++)
        {
            Console.WriteLine(counter + ") " + name + phrase);
        }
    }
}

}

Upvotes: 1

Views: 699

Answers (1)

croxy
croxy

Reputation: 4168

switch(choice)
{
   case 1: ...
      .
      .
      .
   case -1: Console.WriteLine("Put in your own saying: ");
            phrase = " " + Console.ReadLine();
            break;
}

This switch case will ask you when typing "-1" to put in the saying and then writes it into phrase. After the switch case you can proceed like in your above code.

Upvotes: 4

Related Questions