以下是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JeffOrrisSimpleCalculator._1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Simple Calculator");
Console.WriteLine("\t1> Add");
Console.WriteLine("\t2> Subtract");
Console.WriteLine("\t3> Multiply");
Console.WriteLine("\t4> Divide");
Console.WriteLine("\t5> Quit");
Console.WriteLine();
Console.Write("Enter Selection: ");
string myValue = ValidateData();
Console.WriteLine(myValue);
Console.Write("Enter number one: ");
Console.ReadLine();
Console.Write("Enter number two: ");
Console.ReadLine();
string mySum = Add();
Console.WriteLine(mySum);
Console.Read();
}//end main
private static string ValidateData()
{
string temp = Console.ReadLine();
if ((temp == "1") || (temp == "2") || (temp == "3") || (temp == "4") || (temp == "5"))
{
//string Response = "Thank You";
//return Response;
return null;
}
else
{
string Response = "Please enter a valid selection";
return Response;
}
}//end ValidateData
private static string Add()
{
string numberOne = Console.ReadLine();
double temp1 = Convert.ToDouble(numberOne);
string numberTwo = Console.ReadLine();
double temp2 = Convert.ToDouble(numberTwo);
double total = temp1 + number2;
string myTotal = Convert.ToString(total);
return myTotal;
}
}//end class
}//end namespace
*我只关心 add 方法。我提示用户输入我需要使用 add 方法添加的 2 个数字。我相信我想多了。我习惯于 Windows 窗体而不是控制台应用程序。我不知道如何从 Console.ReadLine 连续保存这些输入变量。