I have a problem, I am very newbie and I want to create a calculator in C#, but I have this problem:
static long Suma(ref int x, int y)
{
int z;
z = x + y;
return z;
}
static long Resta(ref int x, int y)
{
int z;
z = x - y;
return z;
}
static long Multi(ref int x, int y)
{
int z;
z = x + y;
return z;
}
static long Divi(ref int x, int y)
{
int z;
z = x + y;
return z;
}
static void Main(string[] args)
{
string op;
int v1;
int v2;
int v3;
int r1;
int r2;
Console.WriteLine("Hola! Inserta el primer valor");
v1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Para una suma, toca '+' Para una resta, toca '-' Para una multiplicacion, toca '*' Para una division, toca '/'");
op = Console.ReadLine();
Console.WriteLine("Dame el segundo valor");
v2 = Convert.ToInt32(Console.ReadLine());
if (op.Equals("+"))
{
r1 = Convert.ToInt32(Suma(ref v1, v2));
Console.WriteLine(r1);
}
v3 = Convert.ToInt32(Console.ReadLine());
r2 = v3 + r1;
Console.WriteLine(r2);
It tells me that r1
it is not assigned, but r1
it is assigned in the first one if
(Obviously the calculator will not be like that, but I put the r2
and everything to test because it did not work for me) In the Console.WriteLine(r1)
If it gives me the value, then if it is assigned, I do not understand .
don't worry about being a rookie, that's how we all started. I just reviewed your problem and a solution I can give you is to assign a value to your variable r1, for example: r1 = 0; Likewise, when performing the calculation, the value will be replaced. Here is the code of the main function: