After learning C language, there is a very small problem that can not be adapted to learning C #. That is, in C, you can enter several numbers or letters in a line and assign them to the corresponding variables individually. But in C # It's not the same, it can only read one line. To implement a variable assignment, only one line is lost. This is not very inconvenient. So I made a few attempts. Let's share it here.
Let's start with a simple line and enter one:
int A1;
A1=system.console.read ();
Console.WriteLine (A1);
It is worth noting here that the output of the A1 is actually an input worth asii value. If you want to output the input value, you need to do the following:
int A1;
A1 =int32.parse (System.Console.ReadLine ());
Console.WriteLine (A1);
This shows that the read () function is primarily used to determine whether the input is finished, and the ReadLine () function can be used to get the value.
String A1;
A1 =system.console.readline ();
Console.WriteLine (A1);
This can also achieve the effect, but at this time the A1 is a string.
The next step is to read one line and assign it one by one.
int[] Arry = new INT[5];
String str1 = "";
STR1 = Console.ReadLine ();
string[] num = str1. Split (new string[] {""}, Stringsplitoptions.none);
int count = 0;
while (Count < Num. Count<string> ())
{
Arry[count] = Int32.Parse (Num[count]);
count++;
}
foreach (int i in Arry)
Console.Write ("{0}", i);
This is primarily about the use of the split () function. The core is character cutting and looping.
Here's a reference to the split () function usage (http://www.cnblogs.com/yugen/archive/2010/08/18/1802781.html) that someone else has written:
String.Split method has 6 overloaded functions: Program code 1) public string[] Split (params char[] separator) 2) public string[] Split (char[] separator, in T count)
3) Public string[] Split (char[] separator, stringsplitoptions options)
4) Public string[] Split (string[] separator, stringsplitoptions options)
5) Public string[] Split (char[] separator, int count, stringsplitoptions options)
6) Public string[] Split (string[] separator, int count, stringsplitoptions options)
Below we have some examples to illustrate how to use (the following string words = "1,2.3,,4";):
1. Public string[] Split (params char[] separator)Program code string[] split = words. Split (new char[] {', '});//return: {"1", "2.3", "", "4"}
string[] split = words. Split (new char[] {', ', '. '}); /return: {"1", "2", "3", "", "4"}
2. Public string[] Split (char[] separator, int count)Program code string[] split = words. Split (new char[] {', ', '. '}, 2);//return: {"1", "2.3,,4"}
string[] split = words. Split (new char[] {', ', '. '}, 6);//return: {"1", "2", "3", "", "4"}
3. Public string[] Split (char[] separator, stringsplitoptions options)Program code string[] split = words. Split (new char[] {', ', '. '}, stringsplitoptions.removeemptyentries);//return: {"1", "2", "3", "4"} do not preserve empty elements
string[] split = words. Split (new char[] {', ', '. '}, Stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Leave empty elements
4. Public string[] Split (string[] separator, stringsplitoptions options)Program code string[] split = words. Split (new string[] {",", "."}, stringsplitoptions.removeemptyentries);//return: {"1", "2", "3", "4"} do not preserve empty elements
string[] split = words. Split (new string[] {",", "."}, Stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Leave empty elements
5. Public string[] Split (char[] separator, int count, stringsplitoptions options)Program code string[] split = words. Split (new char[] {', ', '. '}, 2, stringsplitoptions.removeemptyentries);//return: {"1", "2.3,,4"} does not preserve empty elements
string[] split = words. Split (new char[] {', ', '. '}, 6, stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Leave empty elements
6. Public string[] Split (string[] separator, int count, stringsplitoptions options)Program code string[] split = words. Split (new string[] {",", "."}, 2, stringsplitoptions.removeemptyentries);//return: {"1", "2.3,,4"} does not preserve empty elements
string[] split = words. Split (new string[] {",", "."}, 6, stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Reserved empty elements Note that there is no overloaded function public string[] Split (string[] separator), so we can't use Words.split (",") like vb.net, and only use words. Split (', ')
C # Console input