guys, I'm a beginner at this, after trying to build this calculator I just wanted to learn how to make it start again I tried using a while loop but unfortunately, I wasn't able to figure it out please help thanks.
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Add an operator");
string op = Console.ReadLine();
Console.Write("Enter another 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");
}
}
}
}
If it is console application, you could do something like this at the end of your app -
System.Diagnostics.Process.Start("your_prog_name.exe"); Environment.Exit(0);
It should terminate previous process and start it again. Console window won't disappear.
To keep it running, you can add a while(true)
to wrap your logic code. this will repeatedly execute the block of code. In case there is a condition where you want to exit the loop, you can achieve this via break
static void Main(string[] args)
{
while (true){
Console.Write("Enter a number");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Add an operator");
string op = Console.ReadLine();
Console.Write("Enter another 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");
}
if (//condition is ok and you want to break out of the loop){
break;
}
}
}
thanks so much guys I really appreciate the help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.