c#中的冒泡排序和字符串Replace方法

匿名 (未验证) 提交于 2019-12-02 23:39:01

一.冒泡排序

口诀:
N 个数字来排队,两两相比小靠前。

案例:定义一个数组,输出后从大到小排列

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;  namespace _04冒泡排序 {     class Program     {         static void Main(string[] args)         {             //冒泡排序算法:             //大的往上冒,小的往下沉             //对于一组数进行按照从大到小或从小到大的一个顺序排列算法             int[] array = { 23, 30, 18, 40, 21 };             //控制轮数             for (int i = 0; i < array.Length-1; i++)             {                 for (int j=0;j<array.Length-1- i;j++) {                     if (array[j]<array[j+1])                     {                         int temp = array[j];                         array[j] = array[j + 1];                         array[j + 1] = temp;                     }                 }             }             for (int i = 0; i < array.Length; i++)             {                 Console.Write(array[i] + " ");             }             Console.ReadLine();         }     } } 

二.字符串Replace方法

例如:
在聊天室中经常遇到屏蔽脏话功能,完成当用户输入一句话中带有“sb”,则将“sb”替换成“**”

Console.WriteLine("请输入一句带有sb的话:");             string rep = Console.ReadLine();             string a = rep.Replace("sb", "**");             Console.WriteLine(a);             Console.ReadLine(); 
文章来源: https://blog.csdn.net/weixin_44870681/article/details/91339143
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!