我已閱讀使用文本文件的單位轉換並將它們存儲在array
中。如何根據數組元素從控制檯檢查用戶輸入值? c#
我打算讓他們想要轉換的每個測量的用戶輸入,並將它們與中的element
進行比較,以便我可以引用它們以輸入有效輸入。
在我的陣列中的數據被分割與使用(",")
數組中的數據以這種格式組織。例如「盎司,克,28.0」
如何檢查用戶是否輸入了vald輸入?例如,他們認爲盎司不是另一種不相容的測量,如品脫。
以下是我都有種到目前爲止
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?
}
}
任何幫助,將不勝感激完成。謝謝! 編輯:凹凸
我試過m稍微更清楚地提出這個問題。評論你是否還有其他問題。 – zac