Try to parse user input - CSharp Language Basics

CSharp examples for Language Basics:Data Type

Description

Try to parse user input

Demo Code

using static System.Console;
class Program//from  ww  w  . java  2s .co m
{
   static void TimesTable(byte number)
   {
      WriteLine($"This is the {number} times table");
      for (int row = 1; row <= 12; row++)
      {
         WriteLine($"{row} x {number} = {row * number}");
      }
   }
   static void Main(string[] args)
   {
      Write("Enter a number between 0 and 255: ");
      if (byte.TryParse(ReadLine(), out byte number))
      {
         TimesTable(number);
      }
      else
      {
         WriteLine("You did not enter a valid number!");
      }
   }
}

Result


Related Tutorials