Console calculator for 2 numbers at a time
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I'm learning to code for fun, and just finished my first program.
Idk what to think about it since I it's my first finished real code, but hey it works! I'm pretty sure that it can be written shorter or better than this.
Right now it can only solve 1 problem at a time, but I'm going to make it so it can save the outcome and work further on it and after that I'm of plan to change from console to a real calculator look. I just want to improve myself by listening at your thoughts before working further on it.
What could I do shorter/easier/simpler/better?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
(Action == "-")
c# beginner console calculator
New contributor
add a comment |Â
up vote
6
down vote
favorite
I'm learning to code for fun, and just finished my first program.
Idk what to think about it since I it's my first finished real code, but hey it works! I'm pretty sure that it can be written shorter or better than this.
Right now it can only solve 1 problem at a time, but I'm going to make it so it can save the outcome and work further on it and after that I'm of plan to change from console to a real calculator look. I just want to improve myself by listening at your thoughts before working further on it.
What could I do shorter/easier/simpler/better?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
(Action == "-")
c# beginner console calculator
New contributor
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I'm learning to code for fun, and just finished my first program.
Idk what to think about it since I it's my first finished real code, but hey it works! I'm pretty sure that it can be written shorter or better than this.
Right now it can only solve 1 problem at a time, but I'm going to make it so it can save the outcome and work further on it and after that I'm of plan to change from console to a real calculator look. I just want to improve myself by listening at your thoughts before working further on it.
What could I do shorter/easier/simpler/better?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
(Action == "-")
c# beginner console calculator
New contributor
I'm learning to code for fun, and just finished my first program.
Idk what to think about it since I it's my first finished real code, but hey it works! I'm pretty sure that it can be written shorter or better than this.
Right now it can only solve 1 problem at a time, but I'm going to make it so it can save the outcome and work further on it and after that I'm of plan to change from console to a real calculator look. I just want to improve myself by listening at your thoughts before working further on it.
What could I do shorter/easier/simpler/better?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
(Action == "-")
c# beginner console calculator
c# beginner console calculator
New contributor
New contributor
edited 35 mins ago
200_success
126k14146407
126k14146407
New contributor
asked 2 hours ago
Betrax
334
334
New contributor
New contributor
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
Some rules of thumb:
If you have your entire program in a single method (
Main()
, in this case), there are some design considerations you should probably revisit. Especially from a DRY ("Don't Repeat Yourself") perspective.If you find yourself using
goto
in a language invented after 1985, you're likely not availing yourself of the rich and varied control structures a language has to offer.If you're using C#, you should consider using object-oriented design and implementation. While the language does support other paradigms (such as the imperative form, which you're employing here), it's forte is OO and using it as a tool to solve problems is very much idiomatic.
A good design approach, even in simple programs is to separate the so-called "business logic" from the user interface logic, also known as Separation of Concerns. Keep your calculations away from your
WriteLine
s, as it were.
So, let's take a look at one possible refinement (I'm going to address points 1 and 2):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
while (true)
double number1 = GetNumber();
Console.Clear();
Console.WriteLine(number1);
Console.WriteLine();
string action = GetAction();
double number2 = GetNumber();
Console.WriteLine(number2);
double result = Perform(number1, action, number2);
Console.Clear();
Console.WriteLine(number1 + action + number2);
Console.WriteLine();
Console.WriteLine("= " + result);
Console.Write("Press enter to calculate again: ");
Console.ReadLine();
Console.Clear();
private static double GetNumber()
double number;
Console.Write("Number: ");
while (!double.TryParse(Console.ReadLine(), out number))
NumberInputError();
return number;
private static void NumberInputError()
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
private static string GetAction()
while (true)
private static double Perform(double number1, string action, double number2)
switch (action)
case "+": return number1 + number2;
case "-": return number1 - number2;
case "*": return number1 * number2;
case "/": return number1 / number2;
default: throw new InvalidOperationException("False action input, choose between: / ; * ; - ; +");
1
Thank you very much! You illuminated me by using private static s. Now I know what to learn next and how to improve. THANK YOU VERY MUCH!
â Betrax
6 mins ago
Glad to help - hope to see more refinements and iterations along your journey.
â Jesse C. Slicer
5 mins ago
add a comment |Â
up vote
2
down vote
I think your variant can be improved by reducing the number of code duplication.
My var:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static double Number1;
static string Action;
static double Number2;
static double Answer;
static void Main(string args)
bool AppOn = true;
while (AppOn != false)
Console.WriteLine();
Console.WriteLine();
Console.Write("Number: ");
Number1 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1);
Action = TryGetAction();
Console.Clear();
Console.WriteLine(Number1 + Action);
Number2 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1 + Action + Number2);
switch (Action)
case "*":
Answer = Number1 * Number2;
break;
case "-":
Answer = Number1 - Number2;
break;
case "+":
Answer = Number1 + Number2;
break;
case "/":
Answer = Number1 / Number2;
break;
Console.WriteLine();
Console.WriteLine("= " + Answer);
Console.Write("Press enter to calculate again or esc for exit: ");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Enter)
AppOn = true;
switch (keyInfo.Key)
case ConsoleKey.Enter:
AppOn = true;
break;
case ConsoleKey.Escape:
AppOn = false;
break;
Console.Clear();
public static double TryGetNumber()
double resultNumber;
while (!double.TryParse(Console.ReadLine(), out resultNumber))
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
return resultNumber;
public static string TryGetAction()
Console.WriteLine();
Console.WriteLine();
Console.Write("Action: ");
string result = "";
bool success = false;
while (success != true)
string Action = Console.ReadLine();
if ((Action == "*")
return result;
What i do? Step by step.
- Remove all goto operator. In C# and in some (many) others lang decided that goto operator is evil operator, because in small programs you cant see this hell, but on projects larger code will become so hard for read and write. And in any case you use methods. There are many reasons, I advise you to read about this.
- Remove dublicate.
In your version a lot of code that was created by Ctrl + C, Ctrl + V. Usually try to move away from this, wrapping such pieces in the methods and using several times. Let me show:(same color indicates same piece)
Same pieces, but in my variant
- My version seems to be the same volume , because I added a lot of indents and spaces, focusing on the steps and structuring the code.
I use more things than in your version. I can answer your questions.
New contributor
add a comment |Â
up vote
1
down vote
You should not be repeating. Put it below the the final else.
while (!double.TryParse(Console.ReadLine(), out Number2))
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Some rules of thumb:
If you have your entire program in a single method (
Main()
, in this case), there are some design considerations you should probably revisit. Especially from a DRY ("Don't Repeat Yourself") perspective.If you find yourself using
goto
in a language invented after 1985, you're likely not availing yourself of the rich and varied control structures a language has to offer.If you're using C#, you should consider using object-oriented design and implementation. While the language does support other paradigms (such as the imperative form, which you're employing here), it's forte is OO and using it as a tool to solve problems is very much idiomatic.
A good design approach, even in simple programs is to separate the so-called "business logic" from the user interface logic, also known as Separation of Concerns. Keep your calculations away from your
WriteLine
s, as it were.
So, let's take a look at one possible refinement (I'm going to address points 1 and 2):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
while (true)
double number1 = GetNumber();
Console.Clear();
Console.WriteLine(number1);
Console.WriteLine();
string action = GetAction();
double number2 = GetNumber();
Console.WriteLine(number2);
double result = Perform(number1, action, number2);
Console.Clear();
Console.WriteLine(number1 + action + number2);
Console.WriteLine();
Console.WriteLine("= " + result);
Console.Write("Press enter to calculate again: ");
Console.ReadLine();
Console.Clear();
private static double GetNumber()
double number;
Console.Write("Number: ");
while (!double.TryParse(Console.ReadLine(), out number))
NumberInputError();
return number;
private static void NumberInputError()
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
private static string GetAction()
while (true)
private static double Perform(double number1, string action, double number2)
switch (action)
case "+": return number1 + number2;
case "-": return number1 - number2;
case "*": return number1 * number2;
case "/": return number1 / number2;
default: throw new InvalidOperationException("False action input, choose between: / ; * ; - ; +");
1
Thank you very much! You illuminated me by using private static s. Now I know what to learn next and how to improve. THANK YOU VERY MUCH!
â Betrax
6 mins ago
Glad to help - hope to see more refinements and iterations along your journey.
â Jesse C. Slicer
5 mins ago
add a comment |Â
up vote
3
down vote
accepted
Some rules of thumb:
If you have your entire program in a single method (
Main()
, in this case), there are some design considerations you should probably revisit. Especially from a DRY ("Don't Repeat Yourself") perspective.If you find yourself using
goto
in a language invented after 1985, you're likely not availing yourself of the rich and varied control structures a language has to offer.If you're using C#, you should consider using object-oriented design and implementation. While the language does support other paradigms (such as the imperative form, which you're employing here), it's forte is OO and using it as a tool to solve problems is very much idiomatic.
A good design approach, even in simple programs is to separate the so-called "business logic" from the user interface logic, also known as Separation of Concerns. Keep your calculations away from your
WriteLine
s, as it were.
So, let's take a look at one possible refinement (I'm going to address points 1 and 2):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
while (true)
double number1 = GetNumber();
Console.Clear();
Console.WriteLine(number1);
Console.WriteLine();
string action = GetAction();
double number2 = GetNumber();
Console.WriteLine(number2);
double result = Perform(number1, action, number2);
Console.Clear();
Console.WriteLine(number1 + action + number2);
Console.WriteLine();
Console.WriteLine("= " + result);
Console.Write("Press enter to calculate again: ");
Console.ReadLine();
Console.Clear();
private static double GetNumber()
double number;
Console.Write("Number: ");
while (!double.TryParse(Console.ReadLine(), out number))
NumberInputError();
return number;
private static void NumberInputError()
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
private static string GetAction()
while (true)
private static double Perform(double number1, string action, double number2)
switch (action)
case "+": return number1 + number2;
case "-": return number1 - number2;
case "*": return number1 * number2;
case "/": return number1 / number2;
default: throw new InvalidOperationException("False action input, choose between: / ; * ; - ; +");
1
Thank you very much! You illuminated me by using private static s. Now I know what to learn next and how to improve. THANK YOU VERY MUCH!
â Betrax
6 mins ago
Glad to help - hope to see more refinements and iterations along your journey.
â Jesse C. Slicer
5 mins ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Some rules of thumb:
If you have your entire program in a single method (
Main()
, in this case), there are some design considerations you should probably revisit. Especially from a DRY ("Don't Repeat Yourself") perspective.If you find yourself using
goto
in a language invented after 1985, you're likely not availing yourself of the rich and varied control structures a language has to offer.If you're using C#, you should consider using object-oriented design and implementation. While the language does support other paradigms (such as the imperative form, which you're employing here), it's forte is OO and using it as a tool to solve problems is very much idiomatic.
A good design approach, even in simple programs is to separate the so-called "business logic" from the user interface logic, also known as Separation of Concerns. Keep your calculations away from your
WriteLine
s, as it were.
So, let's take a look at one possible refinement (I'm going to address points 1 and 2):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
while (true)
double number1 = GetNumber();
Console.Clear();
Console.WriteLine(number1);
Console.WriteLine();
string action = GetAction();
double number2 = GetNumber();
Console.WriteLine(number2);
double result = Perform(number1, action, number2);
Console.Clear();
Console.WriteLine(number1 + action + number2);
Console.WriteLine();
Console.WriteLine("= " + result);
Console.Write("Press enter to calculate again: ");
Console.ReadLine();
Console.Clear();
private static double GetNumber()
double number;
Console.Write("Number: ");
while (!double.TryParse(Console.ReadLine(), out number))
NumberInputError();
return number;
private static void NumberInputError()
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
private static string GetAction()
while (true)
private static double Perform(double number1, string action, double number2)
switch (action)
case "+": return number1 + number2;
case "-": return number1 - number2;
case "*": return number1 * number2;
case "/": return number1 / number2;
default: throw new InvalidOperationException("False action input, choose between: / ; * ; - ; +");
Some rules of thumb:
If you have your entire program in a single method (
Main()
, in this case), there are some design considerations you should probably revisit. Especially from a DRY ("Don't Repeat Yourself") perspective.If you find yourself using
goto
in a language invented after 1985, you're likely not availing yourself of the rich and varied control structures a language has to offer.If you're using C#, you should consider using object-oriented design and implementation. While the language does support other paradigms (such as the imperative form, which you're employing here), it's forte is OO and using it as a tool to solve problems is very much idiomatic.
A good design approach, even in simple programs is to separate the so-called "business logic" from the user interface logic, also known as Separation of Concerns. Keep your calculations away from your
WriteLine
s, as it were.
So, let's take a look at one possible refinement (I'm going to address points 1 and 2):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static void Main(string args)
while (true)
double number1 = GetNumber();
Console.Clear();
Console.WriteLine(number1);
Console.WriteLine();
string action = GetAction();
double number2 = GetNumber();
Console.WriteLine(number2);
double result = Perform(number1, action, number2);
Console.Clear();
Console.WriteLine(number1 + action + number2);
Console.WriteLine();
Console.WriteLine("= " + result);
Console.Write("Press enter to calculate again: ");
Console.ReadLine();
Console.Clear();
private static double GetNumber()
double number;
Console.Write("Number: ");
while (!double.TryParse(Console.ReadLine(), out number))
NumberInputError();
return number;
private static void NumberInputError()
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
private static string GetAction()
while (true)
private static double Perform(double number1, string action, double number2)
switch (action)
case "+": return number1 + number2;
case "-": return number1 - number2;
case "*": return number1 * number2;
case "/": return number1 / number2;
default: throw new InvalidOperationException("False action input, choose between: / ; * ; - ; +");
answered 22 mins ago
Jesse C. Slicer
11.1k2739
11.1k2739
1
Thank you very much! You illuminated me by using private static s. Now I know what to learn next and how to improve. THANK YOU VERY MUCH!
â Betrax
6 mins ago
Glad to help - hope to see more refinements and iterations along your journey.
â Jesse C. Slicer
5 mins ago
add a comment |Â
1
Thank you very much! You illuminated me by using private static s. Now I know what to learn next and how to improve. THANK YOU VERY MUCH!
â Betrax
6 mins ago
Glad to help - hope to see more refinements and iterations along your journey.
â Jesse C. Slicer
5 mins ago
1
1
Thank you very much! You illuminated me by using private static s. Now I know what to learn next and how to improve. THANK YOU VERY MUCH!
â Betrax
6 mins ago
Thank you very much! You illuminated me by using private static s. Now I know what to learn next and how to improve. THANK YOU VERY MUCH!
â Betrax
6 mins ago
Glad to help - hope to see more refinements and iterations along your journey.
â Jesse C. Slicer
5 mins ago
Glad to help - hope to see more refinements and iterations along your journey.
â Jesse C. Slicer
5 mins ago
add a comment |Â
up vote
2
down vote
I think your variant can be improved by reducing the number of code duplication.
My var:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static double Number1;
static string Action;
static double Number2;
static double Answer;
static void Main(string args)
bool AppOn = true;
while (AppOn != false)
Console.WriteLine();
Console.WriteLine();
Console.Write("Number: ");
Number1 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1);
Action = TryGetAction();
Console.Clear();
Console.WriteLine(Number1 + Action);
Number2 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1 + Action + Number2);
switch (Action)
case "*":
Answer = Number1 * Number2;
break;
case "-":
Answer = Number1 - Number2;
break;
case "+":
Answer = Number1 + Number2;
break;
case "/":
Answer = Number1 / Number2;
break;
Console.WriteLine();
Console.WriteLine("= " + Answer);
Console.Write("Press enter to calculate again or esc for exit: ");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Enter)
AppOn = true;
switch (keyInfo.Key)
case ConsoleKey.Enter:
AppOn = true;
break;
case ConsoleKey.Escape:
AppOn = false;
break;
Console.Clear();
public static double TryGetNumber()
double resultNumber;
while (!double.TryParse(Console.ReadLine(), out resultNumber))
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
return resultNumber;
public static string TryGetAction()
Console.WriteLine();
Console.WriteLine();
Console.Write("Action: ");
string result = "";
bool success = false;
while (success != true)
string Action = Console.ReadLine();
if ((Action == "*")
return result;
What i do? Step by step.
- Remove all goto operator. In C# and in some (many) others lang decided that goto operator is evil operator, because in small programs you cant see this hell, but on projects larger code will become so hard for read and write. And in any case you use methods. There are many reasons, I advise you to read about this.
- Remove dublicate.
In your version a lot of code that was created by Ctrl + C, Ctrl + V. Usually try to move away from this, wrapping such pieces in the methods and using several times. Let me show:(same color indicates same piece)
Same pieces, but in my variant
- My version seems to be the same volume , because I added a lot of indents and spaces, focusing on the steps and structuring the code.
I use more things than in your version. I can answer your questions.
New contributor
add a comment |Â
up vote
2
down vote
I think your variant can be improved by reducing the number of code duplication.
My var:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static double Number1;
static string Action;
static double Number2;
static double Answer;
static void Main(string args)
bool AppOn = true;
while (AppOn != false)
Console.WriteLine();
Console.WriteLine();
Console.Write("Number: ");
Number1 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1);
Action = TryGetAction();
Console.Clear();
Console.WriteLine(Number1 + Action);
Number2 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1 + Action + Number2);
switch (Action)
case "*":
Answer = Number1 * Number2;
break;
case "-":
Answer = Number1 - Number2;
break;
case "+":
Answer = Number1 + Number2;
break;
case "/":
Answer = Number1 / Number2;
break;
Console.WriteLine();
Console.WriteLine("= " + Answer);
Console.Write("Press enter to calculate again or esc for exit: ");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Enter)
AppOn = true;
switch (keyInfo.Key)
case ConsoleKey.Enter:
AppOn = true;
break;
case ConsoleKey.Escape:
AppOn = false;
break;
Console.Clear();
public static double TryGetNumber()
double resultNumber;
while (!double.TryParse(Console.ReadLine(), out resultNumber))
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
return resultNumber;
public static string TryGetAction()
Console.WriteLine();
Console.WriteLine();
Console.Write("Action: ");
string result = "";
bool success = false;
while (success != true)
string Action = Console.ReadLine();
if ((Action == "*")
return result;
What i do? Step by step.
- Remove all goto operator. In C# and in some (many) others lang decided that goto operator is evil operator, because in small programs you cant see this hell, but on projects larger code will become so hard for read and write. And in any case you use methods. There are many reasons, I advise you to read about this.
- Remove dublicate.
In your version a lot of code that was created by Ctrl + C, Ctrl + V. Usually try to move away from this, wrapping such pieces in the methods and using several times. Let me show:(same color indicates same piece)
Same pieces, but in my variant
- My version seems to be the same volume , because I added a lot of indents and spaces, focusing on the steps and structuring the code.
I use more things than in your version. I can answer your questions.
New contributor
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I think your variant can be improved by reducing the number of code duplication.
My var:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static double Number1;
static string Action;
static double Number2;
static double Answer;
static void Main(string args)
bool AppOn = true;
while (AppOn != false)
Console.WriteLine();
Console.WriteLine();
Console.Write("Number: ");
Number1 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1);
Action = TryGetAction();
Console.Clear();
Console.WriteLine(Number1 + Action);
Number2 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1 + Action + Number2);
switch (Action)
case "*":
Answer = Number1 * Number2;
break;
case "-":
Answer = Number1 - Number2;
break;
case "+":
Answer = Number1 + Number2;
break;
case "/":
Answer = Number1 / Number2;
break;
Console.WriteLine();
Console.WriteLine("= " + Answer);
Console.Write("Press enter to calculate again or esc for exit: ");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Enter)
AppOn = true;
switch (keyInfo.Key)
case ConsoleKey.Enter:
AppOn = true;
break;
case ConsoleKey.Escape:
AppOn = false;
break;
Console.Clear();
public static double TryGetNumber()
double resultNumber;
while (!double.TryParse(Console.ReadLine(), out resultNumber))
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
return resultNumber;
public static string TryGetAction()
Console.WriteLine();
Console.WriteLine();
Console.Write("Action: ");
string result = "";
bool success = false;
while (success != true)
string Action = Console.ReadLine();
if ((Action == "*")
return result;
What i do? Step by step.
- Remove all goto operator. In C# and in some (many) others lang decided that goto operator is evil operator, because in small programs you cant see this hell, but on projects larger code will become so hard for read and write. And in any case you use methods. There are many reasons, I advise you to read about this.
- Remove dublicate.
In your version a lot of code that was created by Ctrl + C, Ctrl + V. Usually try to move away from this, wrapping such pieces in the methods and using several times. Let me show:(same color indicates same piece)
Same pieces, but in my variant
- My version seems to be the same volume , because I added a lot of indents and spaces, focusing on the steps and structuring the code.
I use more things than in your version. I can answer your questions.
New contributor
I think your variant can be improved by reducing the number of code duplication.
My var:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
class Program
static double Number1;
static string Action;
static double Number2;
static double Answer;
static void Main(string args)
bool AppOn = true;
while (AppOn != false)
Console.WriteLine();
Console.WriteLine();
Console.Write("Number: ");
Number1 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1);
Action = TryGetAction();
Console.Clear();
Console.WriteLine(Number1 + Action);
Number2 = TryGetNumber();
Console.Clear();
Console.WriteLine(Number1 + Action + Number2);
switch (Action)
case "*":
Answer = Number1 * Number2;
break;
case "-":
Answer = Number1 - Number2;
break;
case "+":
Answer = Number1 + Number2;
break;
case "/":
Answer = Number1 / Number2;
break;
Console.WriteLine();
Console.WriteLine("= " + Answer);
Console.Write("Press enter to calculate again or esc for exit: ");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Enter)
AppOn = true;
switch (keyInfo.Key)
case ConsoleKey.Enter:
AppOn = true;
break;
case ConsoleKey.Escape:
AppOn = false;
break;
Console.Clear();
public static double TryGetNumber()
double resultNumber;
while (!double.TryParse(Console.ReadLine(), out resultNumber))
Console.Clear();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Wrong sort of input.");
Console.Write("Enter a number: ");
return resultNumber;
public static string TryGetAction()
Console.WriteLine();
Console.WriteLine();
Console.Write("Action: ");
string result = "";
bool success = false;
while (success != true)
string Action = Console.ReadLine();
if ((Action == "*")
return result;
What i do? Step by step.
- Remove all goto operator. In C# and in some (many) others lang decided that goto operator is evil operator, because in small programs you cant see this hell, but on projects larger code will become so hard for read and write. And in any case you use methods. There are many reasons, I advise you to read about this.
- Remove dublicate.
In your version a lot of code that was created by Ctrl + C, Ctrl + V. Usually try to move away from this, wrapping such pieces in the methods and using several times. Let me show:(same color indicates same piece)
Same pieces, but in my variant
- My version seems to be the same volume , because I added a lot of indents and spaces, focusing on the steps and structuring the code.
I use more things than in your version. I can answer your questions.
New contributor
New contributor
answered 11 mins ago
Arantler
1213
1213
New contributor
New contributor
add a comment |Â
add a comment |Â
up vote
1
down vote
You should not be repeating. Put it below the the final else.
while (!double.TryParse(Console.ReadLine(), out Number2))
add a comment |Â
up vote
1
down vote
You should not be repeating. Put it below the the final else.
while (!double.TryParse(Console.ReadLine(), out Number2))
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You should not be repeating. Put it below the the final else.
while (!double.TryParse(Console.ReadLine(), out Number2))
You should not be repeating. Put it below the the final else.
while (!double.TryParse(Console.ReadLine(), out Number2))
answered 1 hour ago
paparazzo
4,8921733
4,8921733
add a comment |Â
add a comment |Â
Betrax is a new contributor. Be nice, and check out our Code of Conduct.
Betrax is a new contributor. Be nice, and check out our Code of Conduct.
Betrax is a new contributor. Be nice, and check out our Code of Conduct.
Betrax is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205906%2fconsole-calculator-for-2-numbers-at-a-time%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password