我编写了一个这样的计算器,我也在这里找到了控制台选项,我仍然只是弄清楚它是如何工作的,问题是添加之后,结果变量中会放置零,因此代码无法正常工作。
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApp3
{
public class Program
{
public static int val1 = 1;
public static int val2 = 2;
public static int result;
public static bool ActionResult = false;
static int Slozhenie(int val1, int val2, int result, bool ActionResult)
{
ActionResult = true;
result = val1 + val2;
return result;
}
static int Vychitanie(int val1, int val2, int result)
{
result = val1 - val2 ;
bool ActionResult = true;
return result;
}
static int Umnozhenie(int val1, int val2, int result)
{
result = val1 * val2;
bool ActionResult = true;
return result;
}
static int Delenie(int val1, int val2, int result)
{
result = val1 / val2;
bool ActionResult = true;
return result;
}
static int SlozhenieAfter(int val2, int result, bool ActionResult)
{
ActionResult = true;
result = result * val2;
return result;
}
public static int Start()
{
ActionResult = true;
// done: // запускает все сначала
string[] menuItems = new string[] { "Сложение", "Вычитание", "Деление", "Умножение" };
Console.WriteLine("Меню\n");
int row = Console.CursorTop;
int col = Console.CursorLeft;
int index = 0;
while (true)
{
DrawMenu(menuItems, row, col, index);
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.DownArrow:
if (index < menuItems.Length - 1)
index++;
break;
case ConsoleKey.UpArrow:
if (index > 0)
index--;
break;
case ConsoleKey.Enter:
switch (index)
{
case 0:
Console.WriteLine("\nРезультат сложения равен: " + Slozhenie(val1, val2, result, ActionResult));
return 1;
case 1:
Console.WriteLine("\nРезультат вычитания равен: " + Vychitanie(val1, val2, result));
return Vychitanie(val1, val2, result);
case 2:
Console.WriteLine("\nРезультат деления равен: " + Delenie(val1, val2, result));
return Delenie(val1, val2, result);
case 3:
Console.WriteLine("\nРезультат умножения равен: " + Umnozhenie(val1, val2, result));
return Umnozhenie(val1, val2, result);
}
break;
}
}
}
public static int StartAfter ()
{
done1: // запускает все сначала
string[] menuItems = new string[] { "Сложение", "Вычитание", "Деление", "Умножение" };
Console.WriteLine("Меню\n");
int row = Console.CursorTop;
int col = Console.CursorLeft;
int index = 0;
while (true)
{
DrawMenu(menuItems, row, col, index);
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.DownArrow:
if (index < menuItems.Length - 1)
index++;
break;
case ConsoleKey.UpArrow:
if (index > 0)
index--;
break;
case ConsoleKey.Enter:
switch (index)
{
case 0:
Console.WriteLine("\nРезультат сложения равен: " + SlozhenieAfter(val2, result, ActionResult));
return 1;
case 1:
Console.WriteLine("\nРезультат вычитания равен: " + Vychitanie(val1, val2, result));
goto done1;
case 2:
Console.WriteLine("\nРезультат деления равен: " + Delenie(val1, val2, result));
goto done1;
case 3:
Console.WriteLine("\nРезультат умножения равен: " + Umnozhenie(val1, val2, result));
goto done1;
}
break;
}
}
}
static void DrawMenu(string[] items, int row, int col, int index)
{
Console.SetCursorPosition(col, row);
for (int i = 0; i < items.Length; i++)
{
if (i == index)
{
Console.BackgroundColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Black;
}
Console.WriteLine(items[i]);
Console.ResetColor();
}
Console.WriteLine();
}
public static void Main(string[] args)
{
while (true) {
if (ActionResult)
{
StartAfter();
}
else
{
Start();
}
}
}
}
}