简体   繁体   English

如何从控制台对照数组元素检查用户输入值? C#

[英]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. 我计划获取他们想要转换的每个度量的用户输入,并将它们与arrayelement进行比较,以便我可以引用它们以输入有效的输入。

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.

相关问题 C#:如何根据.csv中的列检查用户控制台输入? - C#: How to check user console input against a column in a .csv? C# - 如何检查字节值是否与指定标志枚举中的任何标志匹配? - C# - how do I check if a byte value matches against any flags in a specified flags enum? C#控制台应用程序-如何始终从控制台读取输入? - C# Console Application - How do I always read input from the console? c#控制台从用户输入获取带有索引的数组值 - c# console get array value with index from user input C#如何检查用户输入 - C# how can I check users input 如何从 C# 中的输入 Console.ReadLine() 获取两个 integer 值? - How do I get two integer values from an input, Console.ReadLine(), in C#? c#如何在消息框中显示此数组(控制台应用程序) - c# How do i display this array in a messagebox (console application) 使用C#WATIN,如何获取INPUT标签html元素的值? - Using C# WATIN, how do I get the value of a INPUT tag html element? 检查用户是否通过C#中的CalendarDatePicker输入了日期时间值 - Check if users has input datetime value through CalendarDatePicker in C# 如何将控制台应用程序中的变量值传递给 C# 中的 windows 表单应用程序(Visual Studio) - How do i pass a variable value from Console application to windows form application in C# (visual studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM