C# calculator not working

Asked

Viewed 105 times

-1

I created a method that asks the user to type numero1 and numero2 (is a simple calculator algorithm), but this method does not store what the user types, regardless of the operation option the user chooses is always giving 0. I would like to solve this problem.

My current code is:

using System;

namespace Calculator
{
    class Program
    {   
        
        static void Main(string[] args)
        {
            Menu();
        }
        static void Menu()
        {   int numero1 = 0, numero2 = 0;

            Console.Clear();

            Console.WriteLine("Escolha uma operação");
            Console.WriteLine("[1]-Soma"); 
            Console.WriteLine("[2]-Subtração");
            Console.WriteLine("[3]-Divisão");
            Console.WriteLine("[4]-Multiplicação");
            Console.WriteLine("[5]-Sair");
            Console.WriteLine("--------------------");
            Console.WriteLine("Selecione uma opção:");
            
            short opcao = short.Parse(Console.ReadLine());

            MenuEscreva();

            string resulta = Result("O resultado é ");

            switch(opcao)
            {   
                
                case 1: Console.WriteLine(resulta + Soma(numero1, numero2)); break;
                case 2: Console.WriteLine(resulta + Subtracao(numero1, numero2)); break;
                case 3: Console.WriteLine(resulta + Divisao(numero1, numero2)); break;
                case 4: Console.WriteLine(resulta + Multiplicacao(numero1, numero2)); break;
                case 5: System.Environment.Exit(0); break;
                default: Menu(); break;
            }
        }
        static void MenuEscreva() //Menu usado para usuario digitar valores
        {
            Console.WriteLine("Digite um Numero:");
            int numero1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Digite outro Numero: "); 
            int numero2 = int.Parse(Console.ReadLine()); 
        }
        static int Soma(int numero1, int numero2)
        {   
            return (numero1 + numero2);
        }
        static string Result(string result) 
        {
            return result;
        }
        static int Subtracao(int numero1, int numero2)
        {
            return (numero1 - numero2);
        }
        static int Divisao(int numero1, int numero2)
        {
            return (numero1 / numero2);
        }
        static int Multiplicacao(int numero1, int numero2)
        {
            return (numero1 * numero2);
        }
    }
}
  • Instead of using a local variable use a field.

  • I recommend that when you ask a question, you close the scope a little. This will attract more people to answer them and the answer can reach more people. For example, in this case you could have debugged your code a little and asked why a method is not returning the instantiated variables in it. It’s always nice to take a look at Tour

2 answers

1


In the Menuescreva() method you are instantiating the variables numero1 and numero2, which in turn are discarded at the end of the method. These variables only exist in the scope of the method.

When you call some calculator operation method (in switch case), you are passing the variables numero1 and numero2 instantiated at the beginning of the method Menu().

Basically, you are never assigning value to the instantiated variables in the Menu, I believe you can solve this with a new method:

        static int ObterValor()
        {
            Console.WriteLine("Digite um Número:");
            return int.Parse(Console.ReadLine());
        }

this way, your Menu() method would look like this:

 static void Menu()
        {   
            Console.Clear();

            Console.WriteLine("Escolha uma operação");
            Console.WriteLine("[1]-Soma"); 
            Console.WriteLine("[2]-Subtração");
            Console.WriteLine("[3]-Divisão");
            Console.WriteLine("[4]-Multiplicação");
            Console.WriteLine("[5]-Sair");
            Console.WriteLine("--------------------");
            Console.WriteLine("Selecione uma opção:");
            
            short opcao = short.Parse(Console.ReadLine());

            int numero1 = ObterValor();
            int numero2 = ObterValor();

            string resulta = Result("O resultado é ");

            switch(opcao)
            {   
                
                case 1: Console.WriteLine(resulta + Soma(numero1, numero2)); break;
                case 2: Console.WriteLine(resulta + Subtracao(numero1, numero2)); break;
                case 3: Console.WriteLine(resulta + Divisao(numero1, numero2)); break;
                case 4: Console.WriteLine(resulta + Multiplicacao(numero1, numero2)); break;
                case 5: System.Environment.Exit(0); break;
                default: Menu(); break;
            }
        }

0

Within the function MenuEscreva you are creating a new variable and putting the value that is inside, but in the rest of the code you are using the variable numero1 and numero2 that is set with 0. One solution would be to change the function

static int MenuEscreva() //Menu usado para usuario digitar valores
{
    Console.WriteLine("Digite um Numero:");
    int numero1 = int.Parse(Console.ReadLine());
    return numero1;
}

In your code you will do so to fill the numero1 and the numero2:

int numero1 = MenuEscreva();
int numero2 = MenuEscreva();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.