0

我已使用文本文件读取单位转换并将它们存储在array.

我计划获取他们想要转换的每个测量的用户输入并将它们与elementin进行比较,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?

    }

}

任何帮助,将不胜感激。谢谢!编辑:撞

4

2 回答 2

0

要比较数组中的度量单位,请使用 Array.Find 方法,如下所示

static void Main()
{
   string[] array1 = { "kgs", "pounds"};
   string value1 = Array.Find(array1,element => element=="kgs");
   Console.WriteLine(value1);
   Console.ReadKey();
}
于 2013-04-14T18:44:42.387 回答
0

利用try catch

您可以修改它以提供更好的输出错误,例如:文件在行号不正确:

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;
    }
于 2013-04-14T18:31:18.260 回答