E
E
embiid2020-11-19 15:08:20
C++ / C#
embiid, 2020-11-19 15:08:20

How to display a string on a certain selection?

How can you make it so that when you enter a number from 1 to 12, which correspond to the month of the year, it displays a certain phrase?
For example, if the user enters 3, then 3 is March and the console should display "Grass".
This is what you need to create an array, for example, the time of the year, which represents the values ​​of the type: And how to do a check for case? Or is there some other approach possible?
int[] wintr = new int[] { 12, 1, 2 };


static void Main() {
           Console.Write("Enter digit[1, 12]: ");
           int choseDigit = Convert.ToInt32(Console.Read());
      
                switch (choseDigit) {
                    case '1':
                        Console.Write("Снежинка");
                        break;

                    case '2':
                        Console.WriteLine("Травулэнька");
                        break;

                    case '3':
                        Console.Write("Солнышко");
                        break;

                    case '4':
                        Console.WriteLine("Листюшёнок");
                        break;
                default:
                    Console.WriteLine("default");
                    break;
                }
            //}
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
idShura, 2020-11-19
@idShura

And how to check for case?

what check?
Or is there some other approach possible?

Dictionary can be used instead of case

V
Victor Ro, 2020-11-19
@VictorRo

You did everything right, only you have choseDigitthis number, so the quotes are not needed. And labels casecan be grouped:

switch (choseDigit) {
  case 1:
  case 2:
  case 12: Console.WriteLine("Зима");
  break;

   case 3:
   case 4:
   case 5: Console.WriteLine("Весна");
   break;

   case 6:
   case 7:
   case 8: Console.WriteLine("Лето");
   break;

   case 9:
   case 10:
   case 11: Console.WriteLine("осень");
   break;

   default: Console.WriteLine("Нет такого месяца");
   break;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question