开发者

How do I categorize user input in C#

I've learned how to store a single input of data and return a value based on it.

What I want to do is o开发者_Go百科btain multiple inputs from the user, then return a result. But how do I define each input?

Like, if I want the first input to be known as 'stock', and the second as 'value', how do I do that?

I hope I explained my question properly.


string stock = Console.ReadLine();
string value = Console.ReadLine();

.. Or am I interpreting your question incorrectly?

Edit: As a response to your comment:

string input = Console.ReadLine(); //enter stock value
string[] parts = input.split(new String[]{ " " });

stock = parts[1];
value = parts[2];  

If you wish to actually define a "new" variable named "stock" and give it the value "value", you should look into System.Collections.Generic.Dictionary<key, value>


If @ItzWarty's answer isn't quite what you want, this would allow your users to enter multiple values in one line:

string line = Console.ReadLine();

//whatever chars you wish to split on...
string[] inputs = line.Split(new char[] {' ', ',', ';'}); 

string stock = inputs[0];
string value = inputs[1];


You can try this code:

        string name = String.Empty;
        string department = String.Empty;
        int age = 0;


        Console.WriteLine("Please, enter the name of your employee, then press ENTER");
        name = Console.ReadLine();
        Console.WriteLine("Please, enter the department of your employee, then press ENTER");
        department = Console.ReadLine();
        Console.WriteLine("Please, enter the age of your employee, then press ENTER");
        Int32.TryParse(Console.ReadLine(), out age); // default 0


        Console.WriteLine("Name: " + name);
        Console.WriteLine("Department: " + department);
        Console.WriteLine("Age: " + age.ToString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜