C # array [1],

Source: Internet
Author: User

C # array [1],

Array: an array of several variables of the same type. The collections of these sort-by-order data elements are called arrays.
Define an array
Int [] variable name = new int [n];

String [] myStringArray = new string [6];

Int [] myArray = new int [] {1, 3, 5, 7, 9 };
Value: int a = myArray [0];

One-dimensional array
Int [] array = new int [5] {1, 2, 3, 4, 5 };
Int [] array = new int [5];
Array [0] = 1;
Array [1] = 2;
Array [2] = 3;
Array [3] = 4;
Array [4] = 5;
// Array [5] = 6; beyond the index range

Int a = array [2];

String [] ss = new string [3];
Ss [0] = "1 ";
Ss [1] = "2 ";
Ss [2] = "3 ";

String B = ss [2];


Input three names into the Array
String [] name = new string [3];
// Console. Write ("Enter the first person name :");
// Name [0] = Console. ReadLine ();
// Console. Write ("enter the second name :");
// Name [1] = Console. ReadLine ();
// Console. Write ("enter the third name :");
// Name [2] = Console. ReadLine ();
For (int I = 1; I <= 3; I ++)
{
Console. Write ("Enter the personal name {0}:", I );
Name [I-1] = Console. ReadLine ();
}

 

// Create an array based on the number of students in the class, requiring everyone's name to be included
Console. Write ("Enter the number of students in the class :");
Int n = int. Parse (Console. ReadLine ());
String [] name = new string [n];
For (int I = 0; I <n; I ++)
{
Console. Write ("enter the name of the {0} person:", I + 1 );
Name [I] = Console. ReadLine ();
}
Console. WriteLine ("enter the names of all people. Press enter to view the names of all people! ");
Console. ReadLine ();
Console. WriteLine ();
For (int I = 0; I <n; I ++)
{
Console. Write (name [I] + "\ t ");
}
Console. ReadLine ();

 

Enter the number of class members in the console
Put the age of each person into an array,
Calculate the total age of all people
Average age
Maximum Age
Console. Write ("Enter the number of students in the class :");
Int n = int. Parse (Console. ReadLine ());
Int [] age = new int [n];
Int sum = 0;
For (int I = 0; I <n; I ++)
{
Console. Write ("Enter the age of {0} individuals:", I + 1 );
Age [I] = int. Parse (Console. ReadLine ());
Sum + = age [I];
}
Console. WriteLine (sum );
Console. WriteLine (sum/n );
Int agemax = 0;
For (int I = 0; I <n; I ++)
{
If (agemax <age [I])
{
Agemax = age [I];
}
}
Console. WriteLine ("maximum age:" + agemax );

// Bubble sort
For (int I = 0; I <n; I ++)
{
For (int j = I; j <n-1; j ++)
{
If (age [I] <age [j + 1])
{
Int zhong = age [I];
Age [I] = age [j + 1];
Age [j + 1] = zhong;
}
}
}

For (int I = 0; I <n; I ++)
{
Console. WriteLine (age [I]);
}
Console. ReadLine ();

After-school questions

Two-color ball: randomly selected from 33 red balls, sorted in ascending order, and selected from 16 blue balls.

Int [] suiji = new int [6];
Random ran = new Random ();
For (int I = 0; I <6; I ++)
{
Bool B = true;
Int a = ran. Next (1, 34 );
For (int j = 0; j <1; j ++)
{
If (a = suiji [j])
{
B = false;
I --;
}
}
If (B)
{
Suiji [I] =;
}
}
Console. Write ("red ball :");
For (int I = 0; I <6; I ++)
{
Console. Write ("" + suiji [I]);
}
Console. WriteLine ();
For (int a = 0; a <1; a ++)
{
Random B = new Random ();
Int c = ran. Next (1, 17 );
Console. WriteLine ("blue:" + c );
}

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.