Basic Exercise II

Source: Internet
Author: User

15. Implemented using the method: There is an array of strings: {"Malone", "Michael Jordan", "Reggie Miller", "Timdenken", "Kobe Bryant"}, output the longest string.
String[] names = {"Malone", "Michael Jordan", "Reggie Miller", "Timdenken", "Kobe Bryant"};
String max = Getlongest (names);
Console.WriteLine (max);
Console.readkey ();

static string Getlongest (string[] names)
{
String Max=names[0]; The first element of the names array is assigned Max, that is, the first number is the maximum number, and then in the for loop and the other is compared, if there is names[i] value is greater than Max, then the maximum value is re-assigned:


String Max=names[0];
for (int i = 0; i < names. Length; i++)
{
if (Names[i]. Length>max. Length)
{
Max=names[i];
}
}
return Max;
16. To do this: calculate the average of an integer array. {1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10}. Requirement: Calculates the result if there are decimals, then two digits (rounded) after the decimal point are displayed.
Int[] Nums = {1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10};
Double avg = getavg (nums);
AVG = convert.todouble (avg. ToString ("0.00"));
Console.WriteLine (avg);
Console.readkey ();
To find an average of an integral type array
Static double Getavg (int[] nums)
{
Double sum = 0;
for (int i = 0; i < Nums. Length; i++)
{
Sum+=nums[i];
}
Return sum/nums. Length;
}


17. Use the bubble Sort method to sort the integer array {1, 3, 5, 7, 90, 2, 4, 6, 8, 10} in ascending order.
Int[] Nums = {1,3,5,7,90,2,4,6,8,10};
Maopao (Nums);
foreach (int item in nums)
//{
Console.WriteLine (item);
//}
Console.readkey ();

17. Bubble sort
static void Maopao (int[] nums)
{
for (int i = 0; i < Nums. Length-1; i++)
{
for (int j = 0; J <nums. Length-1-j; J + +)
{
if (nums[j]>nums[j+1])
{
int TEMP=NUMS[J];
NUMS[J]=NUMS[J+1];
Nums[j + 1] = temp;
}
}
}
}



18. Write a program for the teacher that uses an array to store 30 students ' test scores, assign a 1-100 random value to each array element, and then calculate the average score.
Okay, let's start by defining an array of length 30.
Int[] Nums=new int[30];
To assign a value to an array by a random number, we should assign a value to each element in the array, using a for loop to implement
Initializing random number objects
Random r = new Random ();
int sum=0;
for (int i = 0; i < Nums. Length; i++)
//{
The resulting random number
int number = R.next (0,100);
Assigns the resulting random number to each element of our array
Nums[i] = number;
Sum+=nums[i];
//}
Console.WriteLine (Sum/nums. Length);
Console.readkey ();




19. There are the following strings: "Patient:" Doctor, I have a very bad cough. "Doctor:" How much do you remember? "Patient:" 75 years old. "Doctor:" 20-year-old cough "patient:" Do not cough. "Doctor:" Cough at the age of 40? "Patient:" also does not cough. "Doctor:" That now does not cough, still have to wait for what when cough? ”"】。 Requirements: Please count the number of occurrences of the word "cough" in this word, and the index position of each "cough".


/* String str = "Patient:" Doctor, I have a bad cough. "Doctor:" How much do you remember? "Patient:" 75 years old. "Doctor:" 20-year-old cough "patient:" Do not cough. "Doctor:" Cough at the age of 40? "Patient:" also does not cough. "Doctor:" That now does not cough, still have to wait for what when cough? ”";


int INDEX=STR. IndexOf ("cough"); where//indexof first appeared
We can directly find the first "word" appears in the position, but the second is on the basis of our first add 1 to find, then when will not find it, can not find it, the return value-1
int count = 1;
while (Index!=-1)
{
count++;
index = str. IndexOf ("cough", index+1);
This will find out the 1, let's make a judgment.
if (index==-1)
{
Break
}
Console.WriteLine ("The location of the cough found at {0} times is {1}", Count,index);
}
Console.readkey ();
*/






20. Put the string "Hello World, Hello Worlds!" "Both ends are stripped, and all other spaces are replaced with a single space, and the result is: Hello world!".
/* String s = "Hello world! ";
Remove space with trim ()
s = S.trim ();
So what do we do next? OK, let's get rid of the string in the German space, and then use a space to connect them and return an array
String[] snew= s.split (new char[]{"},stringsplitoptions.removeemptyentries);
Use a space to connect them with join, and then reassign to the string.
s = string. Join ("", snew);
Print output
Console.WriteLine (s);
Console.readkey ();
*/


21st. Make a console applet. Requirements: The user can enter the name of each student in the console, and when the user enters quit (case insensitive), the program stops accepting the user's input and displays the number of students entered by the user, as well as the name of each student. Effect
A collection of student names
/* list<string> listname = new list<string> ();
while (true)
{
Console.WriteLine ("Please enter the name of the College");
String name = Console.ReadLine ();
Add a name to the collection
if (name. ToLower ()! = "Quit")
{
If the input is not quit, the entered learner name is added to the collection
Listname.add (name);
}
Else
{
Break
}
}


Console.WriteLine ("You have entered the names of {0} trainees", Listname.count);
Print each name
for (int i = 0; i < Listname.count; i++)
{
Console.WriteLine (Listname[i]);
}
Console.readkey ();
*/




22. The title of the above topic, and then add a display surname "King" of the number of classmates, here do not consider compound surname problem.
/* list<string> listname = new list<string> ();
while (true)
{
Console.WriteLine ("Please enter the name of the College");
String name = Console.ReadLine ();
Add a name to the collection
if (name. ToLower ()! = "Quit")
{
If the input is not quit, the entered learner name is added to the collection
Listname.add (name);
}
Else
{
Break
}
}
int count=0;
Console.WriteLine ("You have entered the names of {0} trainees", Listname.count);
Print each name
for (int i = 0; i < Listname.count; i++)
{
if (listname[i][0]== ' king ')
{
count++;
}
Console.WriteLine (Listname[i]);
}
Console.WriteLine ("Surname Wang's classmate has {0}", count);
Console.readkey ();


*/


23. Please reverse the contents of the string array {"China", "USA", "Brazil", "Australia", "Canada"}. Then output the inverted array. The reverse () method of the array cannot be used.
String[] names={"China", "United States", "Brazil", "Australia", "Canada"};
Call the function and pass the array in
Change (names);
foreach (string item in Names)
{
Console.WriteLine (item);
}
Console.readkey ();
23. Reversal
static void Change (string[] names)
{
To invert, you need to compare the length of all elements in the array/2, and then 22.
for (int i = 0; i < names. LENGTH/2; i++)
{
String Temp=names[i];
Names[i] = names[names. Length-1-i];
Names[names. Length-1-i]=temp;
}
}

25. What is the overload of a method
Method names have the same parameters (different parameters are divided into two types: 1. Same number, 2. The same number, the same type)
}

Basic Exercise II

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.