I need some help with my code. Here is how the task looks like:
Develop a console program "Calculator", which reads arithmetic operators in loop iteration, which user inputs ("+", "-", "*", "/") and its operands (real numbers), and then calculates, bringing to Screen result of the operation, which is then used in the next operation as the first operand (as a result, program will be something like a chain of successive calculations). The program must correctly handle errors (division by zero, entering the number instead of arithmetic operators, etc.) and notify the user, without interrupting the work.
This is what I've got so far:
Console.WriteLine("Введите число x");
double x = Convert.ToDouble(Console.ReadLine());
while (true)
{ Console.WriteLine("Введите число y");
double y = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("// Выберите операцию //");
Console.WriteLine("+ - сложение");
Console.WriteLine("- - вычитание");
Console.WriteLine("* - умножение");
Console.WriteLine("/ - деление");
string z = Console.ReadLine();
switch (z)
{
case "+":
Console.WriteLine("// Результат //");
Console.WriteLine(x + y);
break;
case "-":
Console.WriteLine("// Результат //");
Console.WriteLine(x - y);
break;
case "*":
Console.WriteLine("// Результат //");
Console.WriteLine(x * y);
break;
case "/":
if (y == 0)
{
Console.WriteLine("Не дели на 0");
}
else
{
Console.WriteLine("// Результат //");
Console.WriteLine(x / y);
}
break;
}
}
I've successfully added division by zero restriction, but that's about all. I don't know, how to make this "chain of calculations". Of course, I understand that I must use loop, but can't come up with anyting. What I've got not is - two numbers are getting operated and that's all, program stops. I tried adding
do
{
//main program body
Console.WriteLine("Press ENTER to continue");
keyInfo = Console.ReadKey(true);
}
while (keyInfo.Key == ConsoleKey.Enter);
but that only makes the program start from the beginning. I need the result of the first operation to become the first operand and so on. Then user must type something for this program to end.
Sorry for my English, and sorry for my poor c# knowledge, unfortunately, I'm a beginner. Hope to hear soon, thanks in advance.
Instead of directly printing the result of the calculation, store the result into the x
variable, print it out and then ask the user to enter the next y
. This way, x will contain the result of the previous calculation, allowing you to "chain" it to the next one. You only ask for x the first time and ask for y each time the loop repeats.
Console.WriteLine("Введите число x");
double x = Convert.ToDouble(Console.ReadLine());
while (true)
{
Console.WriteLine("Введите число y");
double y = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("// Выберите операцию //");
Console.WriteLine("+ - сложение");
Console.WriteLine("- - вычитание");
Console.WriteLine("* - умножение");
Console.WriteLine("/ - деление");
string z = Console.ReadLine();
switch (z)
{
case "+":
Console.WriteLine("// Результат //");
x = (x + y); //Store the result in x
Console.WriteLine(x);
break;
case "-":
Console.WriteLine("// Результат //");
x = (x - y); //Store the result in x
Console.WriteLine(x);
break;
case "*":
Console.WriteLine("// Результат //");
x = (x * y); //Store the result in x
Console.WriteLine(x);
break;
case "/":
if (y == 0)
{
Console.WriteLine("Не дели на 0");
}
else
{
Console.WriteLine("// Результат //");
x = (x / y);
Console.WriteLine(x);
}
break;
}
}