user8746169
user8746169

Reputation:

Running this calculator in loop in console

I am trying to make this console calculator running in loop until the user want to exit the application. How can i use a for loop with if else statements, is it possible?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

            double sum = 0;

            double num1;
            Console.WriteLine("First number: ");
            num1 = Convert.ToInt32(Console.ReadLine());

            double num2;
            Console.WriteLine("Second number: ");
            num2 = Convert.ToInt32(Console.ReadLine());


            Console.WriteLine("Addisjon eller Subtraksjon?");
            string regneoperasjon = Console.ReadLine();

            if(regneoperasjon == "Subtraksjon")
            {
                Console.WriteLine("Svaret til denne regneoperasjonen er: " + (sum = num1 - num2));
            }
            else if (regneoperasjon == "Addisjon")
            {
                Console.WriteLine("Svaret til denne regneoperasjonen er: " + (sum = num1 + num2));
            }
            Console.WriteLine("Do you want to do an another math operation?");
            Console.ReadLine();
        }
    }
}

Upvotes: 0

Views: 317

Answers (3)

Vadim Martynov
Vadim Martynov

Reputation: 8892

You can add a question for user via Console.ReadKey method and then check his answer:

do
{
    double sum = 0;

    double num1;
    Console.WriteLine("First number: ");
    num1 = Convert.ToInt32(Console.ReadLine());

    double num2;
    Console.WriteLine("Second number: ");
    num2 = Convert.ToInt32(Console.ReadLine());


    Console.WriteLine("Addisjon eller Subtraksjon?");
    string regneoperasjon = Console.ReadLine();

    if(regneoperasjon == "Subtraksjon")
    {
        Console.WriteLine("Svaret til denne regneoperasjonen er: " + (sum = num1 - num2));
    }
    else if (regneoperasjon == "Addisjon")
    {
        Console.WriteLine("Svaret til denne regneoperasjonen er: " + (sum = num1 + num2));
    }

    Console.WriteLine("Calculate the next operation (Y/N)?");
}
while(Console.ReadKey(true).Key == ConsoleKey.Y);

So you get the output like in the screenshot below: enter image description here

Upvotes: 1

Frederik Gheysels
Frederik Gheysels

Reputation: 56944

A for loop is used when you know the (maximum) number of iterations upfront.

In your scenario, a while loop is more apropriate.

do
{
   // Perform your logic here

   Console.WriteLine("Press any key to continue; Q to quit");

   var key = Console.ReadKey();

}while( key != ConsoleKey.Q );

or, use an endless while - loop and break from the loop on a certain condition:

while( true )
{
    // Perform your logic here

    Console.WriteLine("Press any key to continue; Q to quit");

    var key = Console.ReadKey();
    if( key == ConsoleKey.Q ) 
    {
        break;
    }
}

Upvotes: 0

jdweng
jdweng

Reputation: 34429

Try following. I made the writlines a write :

namespace Oppgave3Lesson1
{
    class Program
    {
        static void Main(string[] args)
        {
            while(true)
            {
                double sum = 0;

                double num1;
                Console.Write("First number: (or Exit)");
                string firstNumStr = Console.ReadLine();
                if(firstNumStr == "Exit") break;

                num1 = Convert.ToInt32(firstNumStr);

                double num2;
                Console.Write("Second number: ");
                num2 = Convert.ToInt32(Console.ReadLine());


                Console.Write("Addisjon eller Subtraksjon?");
                string regneoperasjon = Console.ReadLine();

                if(regneoperasjon == "Subtraksjon")
                {
                    Console.Write("Svaret til denne regneoperasjonen er: " + (sum = num1 - num2));
                }
                else if (regneoperasjon == "Addisjon")
                {
                    Console.Write("Svaret til denne regneoperasjonen er: " + (sum = num1 + num2));
                }
            }

Upvotes: 0

Related Questions