wingman007

ConsoleInputOutputDraft

Nov 7th, 2015
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.39 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using System.Threading;
  5.  
  6. class PrintNewLine
  7. {
  8.     enum Color { Yellow = 1, Blue, Green };
  9.     static DateTime thisDate = DateTime.Now;
  10.  
  11.     static void Main()
  12.     {
  13.         /*
  14.         string name = "Marry";
  15.         int year = 1987;
  16.  
  17.         Console.WriteLine("{0} was born in {1}.", name, year);
  18.         // Marry was born in 1987.
  19.         // Interpolated Strings – from C#6 (VS 2015)
  20.         Console.WriteLine($"{name} was born in {year}.");
  21.  
  22.         Console.WriteLine("I am writing now: {0, 10:F2}|{1, 10}",
  23.             2.34567, 3.145676);
  24.  
  25.         int a = Console.Read();
  26.         Console.WriteLine(a);
  27.         Console.WriteLine((char)a);
  28.  
  29.         Console.WriteLine("{0} + {1} = {2}", 1, 2, 1 + 2);
  30.  
  31.         while (true)
  32.         {
  33.             Console.WriteLine(Console.KeyAvailable);
  34.             if (Console.KeyAvailable) {
  35.                 ConsoleKeyInfo cki = Console.ReadKey();
  36.                 Console.WriteLine(cki);
  37.             }
  38.         }
  39.         */
  40.         /*
  41.         ConsoleKeyInfo cki;
  42.         // Prevent example from ending if CTL+C is pressed.
  43.         Console.TreatControlCAsInput = true;
  44.  
  45.         Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
  46.         Console.WriteLine("Press the Escape (Esc) key to quit: \n");
  47.         do
  48.         {
  49.             cki = Console.ReadKey();
  50.             Console.Write(" --- You pressed ");
  51.             if ((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
  52.             if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
  53.             if ((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
  54.             Console.WriteLine(cki.Key.ToString());
  55.         } while (cki.Key != ConsoleKey.Escape);
  56.         */
  57.  
  58.         // int symbol = Console.Read();
  59.         // Console.WriteLine("{0} = {1}", (char)symbol, symbol);
  60.         /*
  61.         if (!Console.IsInputRedirected)
  62.         {
  63.             Console.WriteLine("This example requires that input be redirected from a file.");
  64.             return;
  65.         }
  66.  
  67.         Console.WriteLine("About to call Console.ReadLine in a loop.");
  68.         Console.WriteLine("----");
  69.         String s;
  70.         int ctr = 0;
  71.         do
  72.         {
  73.             ctr++;
  74.             s = Console.ReadLine();
  75.             Console.WriteLine("Line {0}: {1}", ctr, s);
  76.         } while (s != null);
  77.         Console.WriteLine("---");
  78.         */
  79.         /*
  80.         int a = int.Parse(Console.ReadLine());
  81.         int b = int.Parse(Console.ReadLine());
  82.  
  83.         Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
  84.         Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
  85.  
  86.         float f = float.Parse(Console.ReadLine());
  87.         Console.WriteLine("{0} * {1} / {2} = {3}", a, b, f, a * b / f);
  88.         */
  89.         /*
  90.         Console.WriteLine("{0,10:C3}", 1023.4567);
  91.         Console.WriteLine("{0,10:D8}", -12345);
  92.         Console.WriteLine("{0,10:E8}", -1234.5);
  93.         Console.WriteLine("{0,10:F2}", -123.456);
  94.         Console.WriteLine("{0,10:N2}", -1234.56);
  95.         Console.WriteLine("{0,10:P2}", 0.45656);
  96.         Console.WriteLine("{0,10:X5}", 254);
  97.  
  98.         Console.Clear();
  99.  
  100.         // Format a negative integer or floating-point number in various ways.
  101.         Console.WriteLine("Standard Numeric Format Specifiers");
  102.         Console.WriteLine(
  103.             "(C) Currency: . . . . . . . . {0:C}\n" +
  104.             "(D) Decimal:. . . . . . . . . {0:D}\n" +
  105.             "(E) Scientific: . . . . . . . {1:E}\n" +
  106.             "(F) Fixed point:. . . . . . . {1:F}\n" +
  107.             "(G) General:. . . . . . . . . {0:G}\n" +
  108.             "    (default):. . . . . . . . {0} (default = 'G')\n" +
  109.             "(N) Number: . . . . . . . . . {0:N}\n" +
  110.             "(P) Percent:. . . . . . . . . {1:P}\n" +
  111.             "(R) Round-trip: . . . . . . . {1:R}\n" +
  112.             "(X) Hexadecimal:. . . . . . . {0:X}\n",
  113.             -123, -123.45f);
  114.  
  115.         // Format the current date in various ways.
  116.         Console.WriteLine("Standard DateTime Format Specifiers");
  117.         Console.WriteLine(
  118.             "(d) Short date: . . . . . . . {0:d}\n" +
  119.             "(D) Long date:. . . . . . . . {0:D}\n" +
  120.             "(t) Short time: . . . . . . . {0:t}\n" +
  121.             "(T) Long time:. . . . . . . . {0:T}\n" +
  122.             "(f) Full date/short time: . . {0:f}\n" +
  123.             "(F) Full date/long time:. . . {0:F}\n" +
  124.             "(g) General date/short time:. {0:g}\n" +
  125.             "(G) General date/long time: . {0:G}\n" +
  126.             "    (default):. . . . . . . . {0} (default = 'G')\n" +
  127.             "(M) Month:. . . . . . . . . . {0:M}\n" +
  128.             "(R) RFC1123:. . . . . . . . . {0:R}\n" +
  129.             "(s) Sortable: . . . . . . . . {0:s}\n" +
  130.             "(u) Universal sortable: . . . {0:u} (invariant)\n" +
  131.             "(U) Universal full date/time: {0:U}\n" +
  132.             "(Y) Year: . . . . . . . . . . {0:Y}\n",
  133.             thisDate);
  134.  
  135.         // Format a Color enumeration value in various ways.
  136.         Console.WriteLine("Standard Enumeration Format Specifiers");
  137.         Console.WriteLine(
  138.             "(G) General:. . . . . . . . . {0:G}\n" +
  139.             "    (default):. . . . . . . . {0} (default = 'G')\n" +
  140.             "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
  141.             "(D) Decimal number: . . . . . {0:D}\n" +
  142.             "(X) Hexadecimal:. . . . . . . {0:X}\n",
  143.             Color.Green);
  144.         */
  145.         /*
  146.         // Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA"); // en-US
  147.         // Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA"); //
  148.         Thread.CurrentThread.CurrentCulture = new CultureInfo("ro-RO");
  149.         //  CultureInfo.InvariantCulture;
  150.  
  151.         // Console.OutputEncoding = Encoding.ASCII;
  152.         Console.OutputEncoding = Encoding.UTF8;
  153.         Console.WriteLine("Това е кирилица: ☺");
  154.  
  155.         ConsoleKeyInfo cki = Console.ReadKey();
  156.         Console.WriteLine("Key: {0}", cki.KeyChar);
  157.         Console.WriteLine("Modifiers: {0}", cki.Modifiers);
  158.         Console.WriteLine("Key: {0}", cki.Key);
  159.  
  160.         // date
  161.         DateTime today = new DateTime(2015, 11, 7, 11, 23, 34);
  162.         Console.WriteLine("Today is {0:d}", today);
  163.         Console.WriteLine("Today is {0:D}", today);
  164.         Console.WriteLine("Today is {0:t}", today);
  165.         Console.WriteLine("Today is {0:T}", today);
  166.         Console.WriteLine("Today is {0:Y}", today);
  167.         Console.WriteLine("Today is {0:yyyy-MM-dd hh:mm:ss}", today);
  168.  
  169.         // currency
  170.         Console.WriteLine("{0:C2}", 234.4567);
  171.         Console.WriteLine("{0:D8}", 2344567);
  172.         Console.WriteLine("{0:F8}", 23.44567);
  173.         Console.WriteLine("{0:N8}", 3243423.44567);
  174.         Console.WriteLine("{0:X8}", 3243423);
  175.         Console.WriteLine("{0:00.0000000}", 3243423.434);
  176.         Console.WriteLine("{0:00.000#####}", 3243423.434);
  177.  
  178.  
  179.         // enum
  180.         Console.WriteLine("The color is {0:G}", Color.Blue);
  181.         Console.WriteLine("The color is {0:D}", Color.Blue);
  182.         Console.WriteLine("The color is {0:X}", Color.Blue);
  183.  
  184.         Console.SetCursorPosition(40, 20);
  185.         Console.ForegroundColor = ConsoleColor.Red;
  186.         Console.BackgroundColor = ConsoleColor.Blue;
  187.         Console.WriteLine("Here we go");
  188.         */
  189.  
  190.         // Console.WriteLine(Console.Read());
  191.         Console.WriteLine(Convert.ToInt32("1234"));
  192.         Console.WriteLine(Convert.ToSingle("1234,44324"));
  193.         Console.WriteLine(Convert.ToDouble("1234,44324")); // Notice ,
  194.        
  195.  
  196.         int result;
  197.         bool isOk = int.TryParse("32423", out result);
  198.         // Console.WriteLine("First" + Environment.NewLine + "Second" + "First" + Environment.NewLine + "First" + Environment.NewLine + "First" + Environment.NewLine);
  199.         // Environment.NewLine;
  200.         int a = 0;
  201.         string stringA = null;
  202.         do
  203.         {
  204.             Console.Write("Please enter a number between 0 and 500: ");
  205.             stringA = Console.ReadLine();
  206.             // 1. int.Parse(); try catch
  207.             try
  208.             {
  209.                 a = int.Parse(stringA);
  210.             }
  211.             catch (FormatException)
  212.             {
  213.  
  214.                 Console.WriteLine("Please enter a proper number between 0 and 500!");
  215.             }
  216.         } while (!(0 <= a && a <= 500));
  217.  
  218.         Console.WriteLine("The number was: " + a);
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment