我已使用文本文件读取单位转换并将它们存储在array
.
我计划获取他们想要转换的每个测量的用户输入并将它们与element
in进行比较,array
以便我可以引用它们以输入有效输入。
我的数组中的数据使用以下方法拆分(",")
数组中的数据以这种格式组织。例如“盎司,克,28.0”
如何检查用户是否输入了有效输入?例如,他们将盎司放在不是另一种不兼容的测量值(如品脱)的地方。
这是我到目前为止所做的
class Program
{
static void Main(string[] args)
{
Program myProgram = new Program();//call for program to start
myProgram.RunProgram();
}
public string inputA;
PUBLIC STRING InputB;
public decimal inputC;
string[] finalArray;
public class Conversion
{
public string measurementA { set; get; }
public string measurementB { set; get; }
public decimal converFac { set; get; }
}
public void RunProgram()
{
Console.WriteLine("Welcome to to our Conversion Calculator");
ReadFile();//read in text file
GetInput();//get the user input
//Validation();//validate it according to the data in the text file, redirect to getInput is not validated correctly
//Display();//display the result accordingly if validated correctly
//ReDo();//offer a restart or an exit to the program
}
public void ReadFile()
{
List<Conversion> Convert = new List<Conversion>();
using (StreamReader sr = new StreamReader(@"U:\convert.txt"))
{
while (sr.Peek() >= 0)
{
string str;
string[] srArray;
str = sr.ReadLine();
srArray = str.Split(',');
Conversion currentConversion = new Conversion();
currentConversion.measurementA = srArray[0];
currentConversion.measurementB = srArray[1];
currentConversion.converFac = decimal.Parse(srArray[2]);
finalArray = (string[])srArray.Clone();
}
}
}
public void GetInput()
{
Console.Write("Please enter your first unit of Measurement.");
inputA = Convert.ToString(Console.ReadLine());
//what do I put her to make sure that it corresponds with one of the measurementA elements in the array?
}
}
任何帮助,将不胜感激。谢谢!编辑:撞