这是一个控制台单位转换程序,它接受用户指定的测量单位(如磅,盎司),然后是第二个测量单位,最后是一个转换值从一个单位到另一个单位。使用通过'.contains'找到的值
string userUnit1, userUnit2 = "";
int userValue = 0;
,可用于在文本文件中被指定的单位,其内容被分成一个字符串数组中的程序(称为“单元”)。文本文件还包含两个单元之间的“转换值”。在文本文件中,它们看起来像这样:盎司,克,28.0(因为每盎司有28克,这个值也放入数组中)。
是在用户输入与下面的代码片段被检查的单位:
double result = 0;
if (units.Contains(userUnit1) && units.Contains(userUnit2))
{
//??? Something like:
//result = userValue */ value in the array element;
}
基本若以上检查语句,用户进入该单位,我想要做什么的阵列是使用该数组元素中的转换值来转换用户输入的数字。我将如何去做这件事?
编辑:这是将分割文本存储到数组中的代码。
using (read = new StreamReader("C:/Users/Sam Smith/Desktop/convert.txt"))
{
while (!read.EndOfStream)
{
lineFromFile = read.ReadLine();
lineFromFile.Split(' ');
foreach (int i in lineFromFile)
{
count++;
if (count % 3 == 0)
{
units.Add(lineFromFile);
}
}
}
}
您正在错误地使用拆分方法。将返回值保存到一个字符串数组中并迭代该数组。而整个功能没什么意义...... – SimpleVar 2012-03-25 21:08:10