Home > Enterprise >  How do I make a program that gets a letter from the user and outputs something according to the lett
How do I make a program that gets a letter from the user and outputs something according to the lett

Time:12-19

Let me tell you more about this question: So, I want to make a program that gets a letter from the user and according to the letter, if it is "R", outputs "It's Red!" in Red text, if it is "G", outputs "It's Green!" in Green text if it is "B", outputs "It's Blue!" in Blue text! But if it is not R, G, or B, then outputs that there is no color associated with this letter. It is a C# Console App and this is what I've written for now:

            char color;

            Console.WriteLine("Enter a letter (R/G/B): ");
            color = char.Parse(Console.ReadLine());

            if(color==R)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Clear();
                Console.WriteLine("It's Red!");
            }

            else if(color==G)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Clear();
                Console.WriteLine("It's Green!");
            }

            else if (color==B)
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Clear();
                Console.WriteLine("It's Blue!");
            }

            Console.ReadKey();

One more thing to mention is that I don't know how to write that other part where how to make it if "color" doesn't match R or G or B. I would be happy to know how to do that as well It is just a little program I'm making out of boredom.

I tried to make something that if "color" is R, outputs that. But I'm not sure how to do that so any assistance regarding that would be great!

CodePudding user response:

Just copy and paste the following code ^^

Console.WriteLine(@"This task was a little bit changed by me.
Please write me if you need me to upgrade or explain smth.
And Sorry for english mistakes, it's not my mother language.

Enter a letter (R/G/B): ");

while (true) // it's gonna work everytime
{
    try // smth in that "try" might have errors, there are no errors, just because "color" is not a char, but string
    {
        string color = Console.ReadLine().ToLower(); // i changes it from char to string, cause it does not make any sense
        // command to lower makes all to lower case, so ("R" => "r", "BlaBLAbla" => "blablabla")

        // that's your code, but we compare strings with "=="
        if (color == "r")
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();
            Console.WriteLine("It's Red!");
        }
        else if (color == "g")
        {
            Console.BackgroundColor = ConsoleColor.Green;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();
            Console.WriteLine("It's Green!");
        }
        else if (color == "b")
        {
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.ForegroundColor = ConsoleColor.White;
            Console.Clear();
            Console.WriteLine("It's Blue!");
        }
        else // "else" works if any other cases are true, so (if color is not "b", "g", "r")
        {
            Console.WriteLine("You need to chose from a letters (R/G/B): ");
        }
    }
    catch (Exception ex) // in a case of error we print this too
    {
        Console.WriteLine("You need to chose from a letters (R/G/B): ");
    }
}

Console.ReadKey();

CodePudding user response:

Why not use an dictionary (key,value) where key is the letter, and value is the message.

You create it like so var dic = new Dictionary<char, string>();

And use it like so:

//dic.Add("key","Value");
  var dic = new Dictionary<string, string>();
  dic.Add("R","It's Red!");
  dic.Add("G","It's Green!");
  dic.Add("B)","It's Blue!");
  //use .ToUpper() if the key is uppercase in order to ensure its not case sensetive
  if(dic.TryGetValue(color.ToUpper(),out var message))
  {
    //output message
    Console.WriteLine(message);
  }
  else
  {
    //output errormessag
  }

You can add as many key/value as you want for each letter

CodePudding user response:

The following code should do the trick:

char color;

Console.WriteLine("Enter a letter (R/G/B): ");
color = char.Parse(Console.ReadLine());

if(color=='R')
{
    Console.BackgroundColor = ConsoleColor.Red;
    Console.ForegroundColor = ConsoleColor.Black;
    Console.Clear();
    Console.WriteLine("It's Red!");
}

else if(color=='G')
{
    Console.BackgroundColor = ConsoleColor.Green;
    Console.ForegroundColor = ConsoleColor.Black;
    Console.Clear();
    Console.WriteLine("It's Green!");
}

else if (color=='B')
{
    Console.BackgroundColor = ConsoleColor.Blue;
    Console.ForegroundColor = ConsoleColor.White;
    Console.Clear();
    Console.WriteLine("It's Blue!");
}

else
{
    Console.WriteLine("There is no color associated with this letter!");
}

Console.ReadKey();

CodePudding user response:

You just need to add one more else, which will handle scenario where user entered letter other than RGB,

        if(color==R)
        {
           //Code for red color
        }
        else if(color==G)
        {
           //Code for Green Color
        }

        else if (color==B)
        {
           //Code for Blue Color
        }
        else
        {
           //Code for any other color than RGB Goes here
        }
  • Related