Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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.
- */
- using System;
- using System.Threading;
- using System.Globalization;
- class UserChooseType
- {
- static void Main()
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
- string choice;
- char action;
- Console.WriteLine("Choose:");
- Console.WriteLine("1. int");
- Console.WriteLine("2. double");
- Console.WriteLine("3. string");
- Console.WriteLine();
- Console.Write("input type: ");
- choice = Console.ReadLine();
- Console.WriteLine();
- if (choice == "1" || choice == "int" || choice == "1. int")
- {
- action = '1';
- }
- else if (choice == "2" || choice == "double" || choice == "2. double")
- {
- action = '2';
- }
- else if (choice == "3" || choice == "string" || choice == "3. string")
- {
- action = '3';
- }
- else
- {
- action = '0';
- }
- switch (action)
- {
- case '1':
- {
- InputInteger();
- break;
- }
- case '2':
- {
- InputDouble();
- break;
- }
- case '3':
- {
- InputString();
- break;
- }
- default:
- {
- Console.WriteLine("Please choose from the list!");
- break;
- }
- }
- Main();
- }
- static void InputInteger()
- {
- int number, result;
- Console.WriteLine("Enter a number: ");
- while (!(int.TryParse(Console.ReadLine(), out number) && number >= int.MinValue && number <= int.MaxValue))
- {
- Console.WriteLine("Please enter a value between " + int.MinValue + " and " + int.MaxValue + Environment.NewLine);
- Console.WriteLine("Enter a number: ");
- }
- result = number + 1;
- Console.WriteLine("You entered number {0} and the result is {1}" + Environment.NewLine,number, result);
- }
- static void InputDouble()
- {
- double number, result;
- Console.WriteLine("Enter a number: ");
- while (!(double.TryParse(Console.ReadLine(), out number) && number >= double.MinValue && number <= double.MaxValue))
- {
- Console.WriteLine("Please enter a value between " + double.MinValue + " and " + double.MaxValue + Environment.NewLine);
- Console.WriteLine("Enter a number: ");
- }
- result = number + 1;
- Console.WriteLine("You entered number {0:F5} and the result is {1:F5}" + Environment.NewLine, number, result);
- }
- static void InputString()
- {
- string myString, result;
- Console.WriteLine("Enter a string: ");
- myString = Console.ReadLine();
- result = myString + " *";
- Console.WriteLine("Your striing is {0} and the result is {1}" + Environment.NewLine, myString, result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment