samedi 27 février 2021

I am new to programing with C# and have a very simple question

I have a function that calculate numbers that the user inputs and that spits out the answer in the console. It is super simple. The user enters a number, then an operator (+,-,/ and*) and then another number. The function takes (number1) and (number2) and adds, subtracts etc and gives back the answer in the console.

And then the console closes. So it's like a one use calculator.

My question is.. How can I make it so once the user have given the 3 inputs, and the program spits out the answer, the program loops back to "Enter a number", and the process starts again.

Here is the enite code:

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Are you in need to calculate some numbers. y/n? ");
        string ifY = Console.ReadLine();

        if (ifY == "n")
        {
            Console.WriteLine("OK! You do not need a calculator");
        }
        else




            Console.Write("Enter a number: ");
        double num1 = Convert.ToDouble(Console.ReadLine());


        Console.Write("Enter operator: ");
        string op = (Console.ReadLine());


        Console.Write("Enter a number: ");
        double num2 = Convert.ToDouble(Console.ReadLine());


        if (op == "+")
        {
            Console.WriteLine(num1 + num2);
        }
        else if (op == "-")
        {
            Console.WriteLine(num1 - num2);
        }
        else if (op == "/")
        {
            Console.WriteLine(num1 / num2);
        }
        else if (op == "*")
        {
            Console.WriteLine(num1 * num2);
        }
        else
        {
            Console.WriteLine("Invalid operator");
        }




    }
}

}

And I'm sorry if what I am reffering to as Function is incorrect. Please correct me. As I said, completely new to this.

I know it is very "nooby" written. But we all have to start somewhere. Pls don't make fun of me 😅

Aucun commentaire:

Enregistrer un commentaire