A few examples of the basis of the array, very practical, many knowledge points. Output data using an array.

Source: Internet
Author: User

First question: output these three names

string[] name = new string[] {"Messi", "Kaka", "Jong"};
for (int i = 0; i < name. Length-1; i++)
{
Console.Write (name[i]+ "\ t");
}
Console.Write (Name[name. Length-1]);
Console.ReadLine ();

Second question: Each element of an array of integers is processed, if the element is positive, the element is added one, if the element is negative, the element is reduced by one

Int[] Shu=new int[]{1,3,5,-2,-3};
for (int i = 0; i < Shu. Length; i++)
{
if (shu[i]>0)
{
shu[i]++;
}
else if (shu[i]<0)
{
shu[i]--;
}
}
for (int i = 0; i < Shu. Length; i++)
{
Console.WriteLine (Shu [i]);

}//parentheses define the variable, which can only be used within parentheses
Note that the scope of I in the For loop, except for this scope, I cannot use the
Console.ReadLine (); */

Third question: Invert elements of a set of string types

String[] Shu = new string[] {"Hello", "a", "true", "ball", "why", "127"};
for (int i = 0; i < Shu. LENGTH/2; i++)//In the computer, 7/2=3, because it is an integer type
{
String temp = Shu[i];
Shu[i] = Shu[shu. Length-1-i];
Shu[shu. Length-1-I] = temp;
}
for (int i = 0; i < Shu. Length; i++)
{
Console.WriteLine (Shu[i]);
}
Console.ReadLine ();

A few examples of the basis of the array, very practical, many knowledge points. Output data using an array.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.