Advertisement
A4L

Chapter - 10 - Switch State

A4L
Dec 7th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. /*Use a switch statement to handle the different operations in different ways. Allow the user to type in
  8. ’+’ for addition, ’-’ for subtraction, ’*’ for multiplication, ’/’ for division, and ’%’ for remainder. For bonus
  9. points, allow the user to type in ’^’ for a power. (You can compute this using the Math.Pow method.
  10. For example, the following does x 2 : Math.Pow(x, 2);)
  11. Print out the results for the user to see.*/
  12.  
  13.  
  14. namespace Chapter___10___Switch_State
  15. {
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             bool v1 = false; bool v2 = false;
  21.             int v1s = 0; int v2s = 0;
  22.             Console.WriteLine("Please Enter a integer.");
  23.             var userInput = Console.ReadLine();
  24.             if (int.TryParse(userInput, out int value1))  
  25.             {
  26.                 Console.WriteLine($"Acepted Value : {value1}");
  27.                 Int32.TryParse(userInput, out v1s);
  28.                 v1 = true;
  29.  
  30.                 Console.WriteLine("\nPlease Enter another integer.");
  31.                 userInput = Console.ReadLine();
  32.                 if (int.TryParse(userInput, out int value2))
  33.                 {
  34.                     Console.WriteLine($"Acepted Value : {value2}");
  35.                     Int32.TryParse(userInput, out v2s);
  36.                     v2 = true;
  37.                 }
  38.                 else { Console.WriteLine("Please, only enter a integer"); }
  39.             }
  40.             else { Console.WriteLine("Please, only enter a integer"); }
  41.  
  42.             if (v1 && v2 )
  43.             {
  44.                 Console.WriteLine("\n\nPlease Enter a Operator Type (+, -, *, /, %, ^)");
  45.                 userInput = Console.ReadLine();
  46.                 if (userInput == "+" || userInput == "-" || userInput == "*" || userInput == "/" || userInput == "%" || userInput == "^")
  47.                 {
  48.                     switch (userInput)
  49.                     {
  50.                         case "+":
  51.                             Console.WriteLine($"{v1s}+{v2s} = {v1s+v2s}");
  52.                             break;
  53.                         case "-":
  54.                             Console.WriteLine($"{v1s}-{v2s} = {v1s - v2s}");
  55.                             break;
  56.                         case "*":
  57.                             Console.WriteLine($"{v1s}*{v2s} = {v1s * v2s}");
  58.                             break;
  59.                         case "/":
  60.                             Console.WriteLine($"{v1s}/{v2s} = {v1s / v2s}");
  61.                             break;
  62.                         case "%":
  63.                             Console.WriteLine($"{v1s}%{v2s} = {v1s % v2s}");
  64.                             break;
  65.                         case "^":
  66.                             Console.WriteLine($"{v1s}^{v2s} = {Math.Pow(v1s, v2s)}");
  67.                             break;
  68.                     }
  69.                 }
  70.                 else { Console.WriteLine("Please Enter a correct Operator!"); }
  71.             }
  72.             else { Console.WriteLine("\n\nYou didn't enter two numbers!"); }
  73.  
  74.             Console.ReadLine();
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement