L
L
Ledington2021-08-22 18:26:02
C++ / C#
Ledington, 2021-08-22 18:26:02

How to do input validation and draw a table?

Good evening!
Tell me how to check that an integer is entered, and not a fractional or a letter? At the moment, when you enter something that is not a whole program crashes.
And how to draw a table so that there is text inside it in the center with indents along the edges and the width of the table depends on the entered text with a limit on the number of characters?
I'm stuck for now. Guide me please.

Draft code
using System;

namespace ConsoleApp_HW03
{
    class Program
    {
        static void Main(string[] args)
    {
      while (true)
      {
        Console.Write("Введите размерность таблицы: ");

        var table = Console.ReadLine();
        var number = int.Parse(table);

        if (number % 2 == 0 && number <= 6)

        {
          Console.Write($"Введите произвольный текст: ");
          string text;
          while ((text = Console.ReadLine()) != string.Empty)
          {
            switch (text.Length)
            {
              case <= 40:
                Console.WriteLine("В яблочко!");
                break;
              case > 40:
                Console.Write("Перелет. Попробуйте снова: ");
                break;

            }
          }
        }

        else
                {
                    Console.WriteLine($"Неприемлимое значение. Попробуйте еще раз.");
                }

            }
    }
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2021-08-23
@yarosroman

https://docs.microsoft.com/ru-ru/dotnet/api/system...

D
Developer, 2021-08-22
@samodum

Regular \d+

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question