I just started coding c# 4 days ago. I just finsihed my first ever Console Program. I would like to hear some suggestions, on how I could improve my code. I would also like to add that this is my first programming language, so I'm still very new to coding.
Also, the namespace says FourFunctionCalcProgram, but the calculator can dore more than 4 things. I just don't know how to change it.
using System;
using System.Threading.Tasks;
using System.Threading;
using System.Linq.Expressions;
using System.Diagnostics;
using System.Runtime.InteropServices.ComTypes;
namespace FourFunctionCalcProgram
{
class Program
{
static void Main(string[] args)
{
int Age = 0;
string Answer = "Null";
string Answer2 = "Null";
bool NeverFalse = true;
double num = 0;
double num1 = 0;
double num2 = 0;
string fun = "";
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear();
Console.Write("Hello, welcome to this calculator.\nBefore we can begin, I would like to ask you for some basic information.");
Thread.Sleep(5000);
Console.Clear();
Console.Write("First of all, please enter your name here: ");
string Name = Console.ReadLine();
do
{
try
{
Console.Clear();
Console.Write("Ok, hello " + Name + " \nPlease enter your age here: ");
Age = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
Console.WriteLine("Please Try Again");
Console.ForegroundColor = ConsoleColor.Black;
Thread.Sleep(1000);
Console.Clear();
}
} while (Age == 0);
do
{
Console.Clear();
Console.Write("Ok, so your name is " + Name + " And you are " + Age + " Years old? ");
Answer = Console.ReadLine();
if (Answer == "Yes" || Answer == "yes" || Answer == "Yep" || Answer == "yep" || Answer == "Correct" || Answer == "correct" || Answer == "True" || Answer == "true")
{
Console.WriteLine("That's great to hear!");
Thread.Sleep(2000);
break;
}
if (Answer == "No" || Answer == "no" || Answer == "Nope" || Answer == "nope" || Answer == "False" || Answer == "false" || Answer == "Incorrect" || Answer == "incorrect")
{
Console.Write("I'm sorry to hear that, what is incorrect? ");
Answer2 = Console.ReadLine();
if (Answer2 == "Name" || Answer2 == "name" || Answer2 == "My name" || Answer2 == "my name" || Answer2 == "My Name" || Answer2 == "The name" || Answer2 == "The Name" || Answer2 == "the name")
{
Console.Clear();
Console.Write("What is your real name? ");
Name = Console.ReadLine();
}
else if (Answer2 == "Age" || Answer2 == "age" || Answer2 == "My age" || Answer2 == "my age" || Answer2 == "My Age" || Answer2 == "The age" || Answer2 == "The Age" || Answer2 == "the age")
{
try
{
Console.Clear();
Console.Write("What is your real age? ");
Age = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
Console.WriteLine("Please try again");
Thread.Sleep(1000);
Console.Clear();
Console.ForegroundColor = ConsoleColor.Black;
}
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("I'm sorry, but I don't understand what you mean by " + Answer + ". Please try again");
Thread.Sleep(1000);
Console.ForegroundColor = ConsoleColor.Black;
}
} while (NeverFalse);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
for (int i = 10; i <= 100; i += 10)
{
Console.WriteLine("Calculating \n" + i + "%");
Thread.Sleep(500);
Console.Clear();
}
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear();
if (Age <= 18)
{
Console.WriteLine("Sorry, but your are too young to utilise this program. Please come back in ", 18 - Age, " years.");
}
else
{
Console.WriteLine("Welcome to my calculator.\n Please read the README, for all functions");
while (NeverFalse)
{
Console.Write("Which calculator do you want to use? The 1Number or 2Number? ");
Answer = Console.ReadLine();
if (Answer == "1Number")
{
while (NeverFalse)
{
do
{
Console.Clear();
Console.Write("Please enter your number here: ");
try
{
num = Convert.ToDouble(Console.ReadLine());
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
Console.WriteLine("Please try again");
Console.ForegroundColor = ConsoleColor.Black;
Thread.Sleep(1000);
}
} while (num == 0);
Console.Clear();
Console.Write("Now, please enter the function: ");
Answer = Console.ReadLine();
Console.Clear();
switch (Answer)
{
case "ROUND":
Console.WriteLine("= " + GetRound(num));
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
case "SQRT":
Console.WriteLine("= " + GetSquareRoot(num));
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
default:
Console.WriteLine("I don't know what you mean by" + Answer + ". Please try again");
break;
}
}
}
else if (Answer == "2Number")
{
do
{
Console.Clear();
try
{
Console.Write("Please enter your first number: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Please enter the function: ");
fun = Console.ReadLine();
Console.Write("Please enter a second number: ");
num2 = Convert.ToDouble(Console.ReadLine());
}
catch (Exception e)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
Console.WriteLine("Please try again.");
Console.ForegroundColor = ConsoleColor.Black;
Thread.Sleep(2000);
Console.Clear();
}
} while (num1 == 0 || num2 == 0);
switch (fun)
{
case "+":
Console.WriteLine("= " + num1 + num2);
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
case "-":
Console.WriteLine("= ", num1 - num2);
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
case "/":
Console.WriteLine("= " + num1 / num2);
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
case "*":
Console.WriteLine("= " + num1 * num2);
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
case "%":
Console.WriteLine("= " + num1 % num2);
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
case "MAX":
Console.WriteLine("= " + GetMax(num1, num2));
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
case "MIN":
Console.WriteLine("= " + GetMin(num1, num2));
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
case "POW":
Console.WriteLine("= " + GetPow(num1, num2));
Thread.Sleep(1000);
Console.Write("Press any key to continue");
Console.ReadKey();
break;
}
}
else
{
Console.WriteLine("Sorry, I do not understand, Please enter '1Number' or '2Number ");
Thread.Sleep(1000);
Console.Clear();
}
}
}
}
static double GetRound(double num)
{
num = Math.Round(num);
return num;
}
static double GetSquareRoot(double num)
{
num = Math.Sqrt(num);
return num;
}
static double GetMax(double num1, double num2)
{
double max = Math.Max(num1, num2);
return max;
}
static double GetMin(double num1, double num2)
{
double min = Math.Min(num1, num2);
return min;
}
static double GetPow(double BaseNum, double PowNum)
{
double result = 1;
for (int i = 0; i < PowNum; i++)
{
result = result * BaseNum;
}
return result;
}
}
}
Entire Solution
. \$\endgroup\$