C # Basic Learning (0701) Some simple ways to practice

Source: Internet
Author: User

A simple way to find the maximum value of an array

Variable parameters

int max= Getmaxnumbers (101,30)

Static int Getmaxnumbers (paramsint[] PMs) {  int max=pms[0];    for (int i=1; I<pms. length;i++)  {    if(pms[i]>max)    {      max=pms[i];    }  }   return Max;}

An easy way to calculate the 1-100 and

Static int getsum () {  int result=0;    for (int i=1; i<=; i++)  {    result=result+i;  }   return result;}

Calculates 1-100 between all odd and

Private Static int getoddsum () {  int sum=0;    for (int i=1; i<=; i++)  {    if(i%2! =0)      {+ =i;    }  }
return sum;}

Determines whether a given integer is a "prime"

Private Static BOOL Iszhishu (int number ) {  for (int i=2;i<number;i++)  {     if (number%i==0)    {      returnfalse;    }  }   return true ;}

Calculates the number of all prime numbers between 1-100 and (applies to the above function of judging prime numbers)

Private Static int getzhishusum () {  int sum=0;    for (int i=2; i<=; i++)  {    if(Iszhishu (i))    {      sum+ =i;    }  }   return sum;}

Suppose you have a string array that uses the method to output the longest string

Private Static string Getmaxname (string[] names) {  string maxname=names[0];    for (int i=1; i<names. length;i++)  {    if(Names[i]. length>maxname.length)    {      Maxname=names[i];    }  }   return Maxname;}

Several ways of declaring a string array

int [] arr=newint[ten];
int [] arr1=newint[]{, a-ten;
Int[] arr2=new int[3]{10,20,30};
int [] arr3={One,a.

Calculates the average of an integer array

int[] arrint={1,3,5,7, -, -,Wuyi, -};Doubleavg=Getavg (arrint);Private Static DoubleGetavg () {Doublesum=0;  for(intI=0; i<arrint.length;i++) {sum+=Arrint[i]; }  returnMath.Round (sum/(Double) arrint.length,2);//result reserved two decimal places (rounded)}

Sort an array of integers in ascending order by bubbling Sort method

int[] arrint={1,3,5,7, -,2,4,6,8,Ten}; for(intI=0; i<arrint.length-1; i++){   for(intj=arrint.length-1; j>i;j--)  {    if(arrint[j]<arrint[j-1])    {      //from back to front 22 contrast, small put forward      inttmp=Arrint[j]; ARRINT[J]=arrint[j-1]; Arrint[j-1]=tmp; }  }} for(intm=0; n<arrint.length;n++) {Console.WriteLine (Arrint[n]);} Console.readkey ();

Counts the number of occurrences of a specified character in a string

//The IndexOf () method reports the index of the first occurrence of the specified string in this instance (returns 1 if the found character has no item-matching value)//The LastIndexOf () method report specifies that only the index of the last occurrence in this instance is obeyedstringmsg="is just against the open hanging Identity classic style style third planning Republicans and spec don't let go over there functional place";//0 indicates that the index starts at the first character position//Msg. IndexOf ("The", 0)//number of occurrences of the record "intCount=0;intindex=0; while(Index=msg. IndexOf ("of the", index))!=-1)//while the condition is that the value returned when the lookup is not-1{Count++; Console.WriteLine ("{0} occurrences of "" at index location: {1}", Count,index); Index=index+"of the". Length;} Console.WriteLine ("" of" has appeared {0} times in total", Count); Console.readkey ();//count the occurrences of each characterdictionary<Char,int> dict=Newdictionary<Char,int>(); for(intI=0; i<msg. length;i++){  if(!Dict. ContainsKey (Msg[i])) {Dict. ADD (Msg[i],1); }  Else{Dict[msg[i]]++; }}foreach(keyvaluepair<Char,int> Iteminchdict) {Console.WriteLine ("the character {0} appears {1} times", item. Key,item. Value);}

Remove both ends of the string and replace all other spaces in it with a single space

stringmsg="Hello World , you are in season! ";//Trim () method to remove both spaces//split with split method, stringsplitoptions.removeemptyentries remove Spacemsg=Msg. Trim ();string[] words=msg. Split (New Char[] {' '},stringsplitoptions.removeemptyentries); msg=string. Join (" ", words); Console.WriteLine ("===="+msg+"===="); Console.readkey ();

C # Basic Learning (0701) Some simple ways to practice

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.