c#基礎學習(0701)之一些簡單的方法練習


一個簡單的求數組最大值的方法

//可變參數

int max=GetMaxNumbers(101,30)

static int GetMaxNumbers(params int[] pms)
{
  int max=pms[0];
  for(int i=1;i<pms.Length;i++)
  {
    if(pms[i]>max)
    {
      max=pms[i];
    }
  }
  return max;
}

一個簡單的方法計算1-100的和

static int GetSum()
{
  int result=0;
  for(int i=1;i<=100; i++)
  {
    result=result+i;
  }
  return result;
}

計算1-100之間所有奇數的和

private static int GetOddSum()
{
  int sum=0;
  for(int i=1;i<=100;i++)
  {
    if(i%2!=0)
    {
      sum +=i;
    }
  }
return sum; }

判斷一個給定的整數是否為“質數”

private static bool IsZhiShu(int number)
{
  for(int i=2;i<number;i++)
  {
    if(number%i==0)
    {
      return false;
    }
  }
  return true;
}

計算1-100之間所有質數的和(運用到了上面的判斷質數的函數)

private static int GetZhiShuSum()
{
  int sum=0;
  for(int i=2;i<=100;i++)
  {
    if(IsZhiShu(i))
    {
      sum+=i;
    }
  }
  return sum;
}

假設有一個字符串數組,用方法輸出最長的字符串

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;
}

字符串數組的幾種聲明方式

int[] arr=new int[10];
int[] arr1=new int[]{10,20,30};
int[] arr2=new int[3]{10,20,30};
int[] arr3={11,22,33};

計算一個整型數組的平均值

 

int[] arrInt={1,3,5,7,90,20,51,53};
double avg=GetAvg(arrInt);
private static double GetAvg()
{
  double sum=0;
  for(int i=0; i<arrInt.Length;i++)
  {
    sum+=arrInt[i];
  }
  return Math.Round(sum/(double)arrInt.Length,2);//結果保留兩位小數(四舍五入)
}

 

通過冒泡排序法對整數數組實現升序排序

int[] arrInt={1,3,5,7,90,2,4,6,8,10};
for(int i=0;i<arrInt.Length-1;i++)
{
  for(int j=arrInt.Length-1;j>i;j--)
  {
    if(arrInt[j]<arrInt[j-1])
    {
      //從后往前兩兩對比,小的放前面
      int tmp=arrInt[j];
      arrInt[j]=arrInt[j-1];
      arrInt[j-1]=tmp;
    }
  }
}
for(int m=0;n<arrInt.Length;n++)
{
  Console.WriteLine(arrInt[n]);
}
Console.ReadKey();

統計一個字符串中出現某個指定字符的次數

//IndexOf()方法報告指定字符串在此實例中的第一個匹配項的索引(如果查找的字符沒有項匹配的值,則返回-1)
//LastIndexOf()方法報告指定只服從在此實例中的最后一個匹配項的索引
string msg="剛好是反對開掛的身份古典風格的風格第三個的規划的共和黨和規范不放過那邊的功能地方";
//0表示該索引從第幾個字符位置開始
//msg.IndexOf("的",0)
//紀錄"的"出現的次數
int count=0;
int index=0;
while((index=msg.IndexOf("",index))!=-1)//while的條件就是查找時的返回的值不是-1
{
  count++;
  Console.WriteLine("第{0}次出現【的】的索引位置為:{1}",count,index);
  index=index+"".Length;
}
Console.WriteLine("【的】總共出現了{0}次",count);
Console.ReadKey();
//統計每個字符出現的次數
Dictionary<char,int> dict=new Dictionary<char,int>();
for(int i=0;i<msg.Length;i++)
{
  if(!dict.ContainsKey(msg[i]))
  {
    dict.Add(msg[i],1);
  }
  else
  {
    dict[msg[i]]++;
  }
}
foreach(KeyValuePair<char,int> item in dict)
{
  Console.WriteLine("字符{0}出現了{1}次",item.Key,item.Value);
}

將字符串的兩端空格去掉,並且將其中的所有其他空格都替換成一個空格

 

string msg="    hello    world ,  你   好時節    !  ";
//Trim()方法去掉兩端空格
//用Split方法分割,StringSplitOptions.RemoveEmptyEntries去除空格
msg=msg.Trim();
string[] words=msg.Split(new char[] {' '},StringSplitOptions.RemoveEmptyEntries);
msg=string.Join(" ",words);
Console.WriteLine("===="+msg+"====");
Console.ReadKey();

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM