C#实现随机抽奖和冒泡排序

随机抽奖程序

  string[] s = new string[] { "A", "B", "C", "D", "E", "F", "G" };
  Random rand = new Random();

  for (int i = 0; i < 50; i++)
    {
      int n = rand.Next(s.Length);//随机数赋值
      Console.WriteLine(s[n]);
      System.Threading.Thread.Sleep(50);//线程等待ms
      Console.Clear();//清除屏幕,把计时的数也清除了

    }

  int m = rand.Next(s.Length);
  Console.WriteLine("恭喜" + s[m] + "中奖");

冒泡排序程序

  int[] m = new int[8];

  Console.WriteLine("任意输入" + m.Length + "个正整数排序");

  for (int i = 1; i <= m.Length; i++)
  {
    m[i - 1] = Convert.ToInt32(Console.ReadLine());//给数组输入数据
  }
  int mid;//定义中间变量
  for (int i = 0; i < m.Length; i++)
  {
    for (int j = 0; j < m.Length - i - 1; j++)
    {
      if (m[j] < m[j + 1]) //将大的数放到前面
      {
        mid = m[j];
        m[j] = m[j + 1];
        m[j + 1] = mid;
      }
    }
  }
  for (int i = 0; i < m.Length; i++)
  {
    Console.Write(m[i] + ","); //输出排好序的数组
  }

上一篇:Python编程中常用的12种基础知识总结


下一篇:STM32串口通信USART1转USART2问题解决