lmarkov

User Choose Type

Dec 4th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. /*
  2.  * Write a program that, depending on the user's choice inputs int, double or string variable. If the variable is integer or double, increases it with 1. If the variable is string, appends "*" at its end. The program must show the value of that variable as a console output. Use switch statement.
  3. */
  4.  
  5. using System;
  6. using System.Threading;
  7. using System.Globalization;
  8.  
  9. class UserChooseType
  10. {
  11.     static void Main()
  12.     {
  13.         Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
  14.  
  15.         string choice;
  16.         char action;
  17.  
  18.         Console.WriteLine("Choose:");
  19.         Console.WriteLine("1. int");
  20.         Console.WriteLine("2. double");
  21.         Console.WriteLine("3. string");
  22.         Console.WriteLine();
  23.         Console.Write("input type: ");
  24.  
  25.         choice = Console.ReadLine();
  26.         Console.WriteLine();
  27.         if (choice == "1" || choice == "int" || choice == "1. int")
  28.         {
  29.             action = '1';
  30.         }
  31.         else if (choice == "2" || choice == "double" || choice == "2. double")
  32.         {
  33.             action = '2';
  34.         }
  35.         else if (choice == "3" || choice == "string" || choice == "3. string")
  36.         {
  37.             action = '3';
  38.         }
  39.         else
  40.         {
  41.             action = '0';
  42.         }
  43.  
  44.         switch (action)
  45.         {
  46.             case '1':
  47.                 {
  48.                     InputInteger();
  49.                     break;
  50.                 }
  51.             case '2':
  52.                 {
  53.                     InputDouble();
  54.                     break;
  55.                 }
  56.             case '3':
  57.                 {
  58.                     InputString();
  59.                     break;
  60.                 }
  61.             default:
  62.                 {
  63.                     Console.WriteLine("Please choose from the list!");
  64.                     break;
  65.                 }                
  66.         }
  67.         Main();
  68.     }
  69.  
  70.     static void InputInteger()
  71.     {
  72.         int number, result;
  73.         Console.WriteLine("Enter a number: ");
  74.         while (!(int.TryParse(Console.ReadLine(), out number) && number >= int.MinValue && number <= int.MaxValue))
  75.         {
  76.             Console.WriteLine("Please enter a value between " + int.MinValue + " and " + int.MaxValue + Environment.NewLine);
  77.             Console.WriteLine("Enter a number: ");
  78.         }
  79.         result = number + 1;
  80.         Console.WriteLine("You entered number {0} and the result is {1}" + Environment.NewLine,number, result);
  81.     }
  82.  
  83.     static void InputDouble()
  84.     {
  85.         double number, result;
  86.         Console.WriteLine("Enter a number: ");
  87.         while (!(double.TryParse(Console.ReadLine(), out number) && number >= double.MinValue && number <= double.MaxValue))
  88.         {
  89.             Console.WriteLine("Please enter a value between " + double.MinValue + " and " + double.MaxValue + Environment.NewLine);
  90.             Console.WriteLine("Enter a number: ");
  91.         }
  92.         result = number + 1;
  93.         Console.WriteLine("You entered number {0:F5} and the result is {1:F5}" + Environment.NewLine, number, result);
  94.     }
  95.  
  96.     static void InputString()
  97.     {
  98.         string myString, result;
  99.         Console.WriteLine("Enter a string: ");
  100.         myString = Console.ReadLine();
  101.         result = myString + " *";
  102.         Console.WriteLine("Your striing is {0} and the result is {1}" + Environment.NewLine, myString, result);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment