les dejo aquí mi calculador a con la sentencia switch: para el que la quiera ampliar o modificar:
using System;
using System.Collections.Generic;
using System.Linq;
namespace CalculatorMethods
{
internal class Program
{
public void IntegerAddition(int a, int b)
{
int addition = a + b;
Console.WriteLine("Result of the integer addition: " + addition);
}
public void Division(float a, float b)
{
if (b != 0)
{
float result = a / b;
Console.WriteLine("Result of the division: " + result);
}
else
{
Console.WriteLine("Error: Cannot divide by zero.");
}
}
public void Multiplication(int a, int b)
{
int multiplication = a * b;
Console.WriteLine("Result of the integer multiplication: " + multiplication);
}
public void SquareRoot(double a)
{
if (a >= 0)
{
double squareRoot = Math.Sqrt(a);
Console.WriteLine("Square root: " + squareRoot);
}
else
{
Console.WriteLine("Error: Cannot calculate the square root of a negative number.");
}
}
static void Main(string[] args)
{
Program calculator = new Program();
char option;
do
{
Console.WriteLine("Select an option:");
Console.WriteLine("1. Integer addition");
Console.WriteLine("2. Division of numbers");
Console.WriteLine("3. Integer multiplication");
Console.WriteLine("4. Square root");
Console.WriteLine("5. Exit");
option = Console.ReadKey().KeyChar;
Console.WriteLine();
switch (option)
{
case '1':
Console.Write("Enter the first integer: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second integer: ");
int num2 = Convert.ToInt32(Console.ReadLine());
calculator.IntegerAddition(num1, num2);
break;
case '2':
Console.Write("Enter the numerator: ");
float numerator = Convert.ToSingle(Console.ReadLine());
Console.Write("Enter the denominator: ");
float denominator = Convert.ToSingle(Console.ReadLine());
calculator.Division(numerator, denominator);
break;
case '3':
Console.Write("Enter the first integer: ");
int factor1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second integer: ");
int factor2 = Convert.ToInt32(Console.ReadLine());
calculator.Multiplication(factor1, factor2);
break;
case '4':
Console.Write("Enter the number to calculate the square root: ");
double number = Convert.ToDouble(Console.ReadLine());
calculator.SquareRoot(number);
break;
case '5':
Console.WriteLine("Exiting the program.");
break;
default:
Console.WriteLine("Invalid option. Please select a valid option.");
break;
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
Console.Clear();
} while (option != '5');
}
}
}