[英]How do I check a users input value from the console against an array element? c#
I have read in unit conversions using a text file and stored them in an array
. 我已经阅读了使用文本文件的单位转换并将其存储在
array
。
I plan to get the user input of each measurement they would like to convert and compare them to the element
in the array
so I can quote them to enter a valid input. 我计划获取他们想要转换的每个度量的用户输入,并将它们与
array
的element
进行比较,以便我可以引用它们以输入有效的输入。
The data in my array is split with the use of (",")
使用
(",")
拆分数组中的数据
The data in the array is organised in this format. 数组中的数据以这种格式组织。 eg "ounce,gram,28.0"
例如“盎司,克,28.0”
How do I check that the user has entered a vald input? 如何检查用户是否输入了密码输入? For example they put ounce where ounce was not another incompatible measurement like pint.
例如,他们把盎司放在不是品脱之类的另一种不兼容的度量标准上。
Here is what I have kind of done so far 到目前为止,这是我所做的事情
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?
}
}
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks!
谢谢! EDIT: Bump
编辑:凹凸
use try
catch
使用
try
catch
You Can modify this to give better output error like :File is incorrect at line number: 您可以修改它以提供更好的输出错误,例如:文件在行号不正确:
public List<Conversion> 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];
try{
currentConversion.converFac = decimal.Parse(srArray[2]);
}
catch(Exception ex){
Console.WriteLine("File is not in correct format");
break;
}
Convert.Add(currentConversion);
}
}
return Convert;
}
To compare the units of measurement in a array, use the Array.Find method as shown below 要比较数组中的度量单位,请使用Array.Find方法,如下所示
static void Main()
{
string[] array1 = { "kgs", "pounds"};
string value1 = Array.Find(array1,element => element=="kgs");
Console.WriteLine(value1);
Console.ReadKey();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.