您好,登錄后才能下訂單哦!
C#中實現微信紅包功能的方法是什么?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!
Program.cs代碼:
class Program { static void Main(string[] args) { //初始化要發起的紅包基礎數據 double total = 100; int num = 50; double min = 0.01; string temp; bool flag = false; Console.WriteLine(string.Format("是否需要自定義紅包金額和數量(默認{0}元/{1}人)Y/N:", total, num)); temp = Console.ReadLine(); if (temp.Trim().ToLower().Equals("y") || temp.Trim().ToLower().Equals("yes")) { Console.WriteLine("請輸入你要發起的紅包金額:"); do { temp = Console.ReadLine(); flag = double.TryParse(temp, out total); if (!flag) { Console.WriteLine("金額必須為整數或小數,請重新輸入:"); } } while (!flag); Console.WriteLine("請輸入你要發起的紅包個數:"); do { temp = Console.ReadLine(); flag = int.TryParse(temp, out num); if (!flag) { Console.WriteLine("紅包個數必須為整數,請重新輸入:"); } } while (!flag); } total -= min * num; if (total < 0) { Console.WriteLine("抱歉,你的金額不足!"); return; } //產生正態分布的隨機紅包金額,并計算相關的金額和數量保證數據的準確性 double average = total / num; double variance = 1; Random u1 = new Random(); Random u2 = new Random(); double[] nums = new double[num]; for (int i = 0; i < num; i++) { double? result = total; if (i < num - 1 && total > 0) { do { result = Round((double)Normal(u1.NextDouble(), u2.NextDouble(), average, variance), 2); } while (result == null || result < 0); if (total > result) { total = (double)Round((total - (double)result), 2); } else { result = total; total = 0; } } else if (i == num - 1) { total = 0; } nums[i] = Math.Round(min + (double)result, 2); //浮點運算問題,這里需要四舍五入數據才正確 Console.WriteLine(string.Format("第{0}個紅包金額:{1}", i + 1, (min + result))); Console.WriteLine("剩余金額:" + ((i != num - 1 && total == 0) ? min * (num - i - 1) : total + (min * (num - i - 1)))); } Console.WriteLine("最大金額:" + nums.Max()); Console.WriteLine("最小金額:" + nums.Min()); Console.WriteLine("總額:" + Round(nums.Sum(), 2)); Console.WriteLine("初始方差:" + variance); Console.WriteLine("結果方差:" + Variance(nums)); Console.WriteLine("按任意鍵退出!"); Console.ReadKey(); } /// <summary> /// 產生符合正態分布的隨機數 /// </summary> /// <param name="u1">正態分布第一個隨機數</param> /// <param name="u2">正態分布第二個隨機數</param> /// <param name="averageValue">正態期望(平均值)</param> /// <param name="variance">正態標準差(Math.Sqrt(方差))</param> /// <returns></returns> public static double? Normal(double u1, double u2, double averageValue, double variance) { double? result = null; try { result = averageValue + Math.Sqrt(variance) * Math.Sqrt((-2) * Math.Log(u1)) * Math.Sin(2 * Math.PI * u2); } catch (Exception) { result = null; } return result; } /// <summary> /// 求一組數據的方差 /// </summary> /// <param name="list">要求的數組</param> /// <returns></returns> public static double Variance(double[] nums) { double average = nums.Sum() / nums.Length; double sum = 0; double variance = 0; foreach (double num in nums) { sum += Math.Pow((num - average), 2); } variance = sum / nums.Length; return variance; } /// <summary> /// 截取小數指定小數位,且不四舍五入 /// </summary> /// <param name="originNum">要截取的小數</param> /// <param name="lastNum">截取小數后位數</param> /// <returns></returns> public static double? Round(double originNum, int lastNum) { double? result = null; int index = originNum.ToString().IndexOf('.'); if (index != -1) { string temp = originNum.ToString(); result = Convert.ToDouble(temp.Substring(0, index + 1) + temp.Substring(index + 1, Math.Min(temp.Length - index - 1, lastNum))); } if (result == 0) { result = null; } else if (index == -1) { result = originNum; } return result; } }
運行結果:
感謝各位的閱讀!看完上述內容,你們對C#中實現微信紅包功能的方法是什么大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。