C#刮刮乐游戏循环异常与总奖金显示错误求助
C#刮刮乐游戏问题修复
现存问题分析与修复
1. 输入“Y”时控制台持续加载故障
原因:选择继续刮奖时未生成新奖券实例,仅重复显示旧奖券内容,同时循环逻辑判断顺序错误,导致输入Y后陷入重复执行的死循环。
修复:用户输入Y时,创建全新的LottoGame实例生成新奖券,同时调整循环内的输入判断逻辑,避免重复触发输入请求。
2. 输入“N”时总奖金显示错误(Total Winnings: $System.Action1[System.Int32]`)
原因:代码错误地直接拼接方法lottoGame.UpdateTotalWinnings而非访问属性;且CheckForWinner方法中return语句在UpdateTotalWinnings之前,导致累计奖金更新逻辑从未执行。
修复:
- 调整
CheckForWinner代码顺序,先执行累计奖金更新再返回匹配次数 - 结束游戏时直接访问
lottoGame.TotalWinnings属性展示累计奖金
3. totalWinners变量的正确使用位置
totalWinners用于统计玩家中奖次数(或累计匹配号码总次数),可在CheckForWinner方法中更新:每当检测到有匹配号码时(numberOfMatches > 0),将totalWinners加1,实现中奖次数统计;若需累计匹配总次数,则直接将numberOfMatches累加到totalWinners即可。
修复后的完整代码
using System; class LottoGame { private int[] numbers = new int[15]; private int prize; private int winningNumber; private int totalWinnings; private int totalWinners; // 统计中奖次数 private string playerInput; public LottoGame() { ShowRulesAndTicket(); GenerateWinningNumber(); GenerateRandomNumbers(); GeneratePrizeValue(); DisplayWinNumAndPrize(); DisplayNumbers(); CheckForWinner(); } public string PlayerInput { get { return playerInput; } set { playerInput = value.ToUpper(); } } public int[] Numbers { get { return numbers; } } public int Prize { get { return prize; } } public int WinningNumber { get { return winningNumber; } } public int TotalWinnings { get { return totalWinnings; } } public int TotalWinners { get { return totalWinners; } } public void ShowRulesAndTicket() { Console.Clear(); Console.WriteLine("***************** Scratch-Off Lottery Game *******************"); Console.WriteLine("Rules:"); Console.WriteLine("\n\n You are shown your *Winning Number* and the potential prize earnings"); Console.WriteLine("You will be given 15 numbers to scratch off and test your luck."); Console.WriteLine("Multiply the prize value times number of matches to the winning number!"); Console.WriteLine("GOOD LUCK!\n\n"); Console.WriteLine("\n\t Winning Prize"); Console.WriteLine("\n\t Number\n"); Console.WriteLine($" ? $?\n"); for (int x = 0; x < 15; x++) { if (x % 5 == 0) Console.WriteLine(); Console.Write($" ? "); } Console.WriteLine(); Console.WriteLine("\n\n\tPress Enter to scratch off numbers...."); Console.ReadLine(); } public void GenerateWinningNumber() { Random rand = new Random(); winningNumber = rand.Next(20) + 10; } public void GenerateRandomNumbers() { Random rand = new Random(); for (int x = 0; x < 15; x++) { numbers[x] = rand.Next(20) + 10; } } public void GeneratePrizeValue() { Random rand = new Random(); int num = rand.Next(3) + 1; if (num == 1) prize = 5; else if (num == 2) prize = 20; else prize = 50; } public void DisplayWinNumAndPrize() { Console.Clear(); Console.WriteLine("***************** Scratch-Off Lottery Game *******************"); Console.WriteLine("\n\t Winning Prize"); Console.Write($" {winningNumber} ${prize}\n"); } public void DisplayNumbers() { for (int x = 0; x < 15; x++) { if (x % 5 == 0) Console.WriteLine(); Console.Write($" {numbers[x]} "); } Console.WriteLine(); int matches = CheckForWinner(); Console.WriteLine($"You won: ${matches * prize}"); } public int CheckForWinner() { int numberOfMatches = 0; for (int x = 0; x < 15; x++) { if (numbers[x] == winningNumber) numberOfMatches++; } // 更新累计奖金 UpdateTotalWinnings(numberOfMatches * prize); // 更新中奖次数:有匹配则计数+1 if (numberOfMatches > 0) totalWinners++; return numberOfMatches; } public void UpdateTotalWinnings(int val) { totalWinnings += val; Console.WriteLine($"Total Winnings so far: ${totalWinnings}"); } } internal class Program { private static void Main(string[] args) { bool gameLoop = true; LottoGame lottoGame = new LottoGame(); while (gameLoop) { Console.WriteLine("\nDo you want to scratch off another ticket Y/N?: "); string input = Console.ReadLine().ToUpper(); if (input == "Y") { // 创建新实例生成全新奖券 lottoGame = new LottoGame(); } else if (input == "N") { gameLoop = false; Console.WriteLine($"\nTotal Winnings: ${lottoGame.TotalWinnings}"); Console.WriteLine($"Total Winning Tickets: {lottoGame.TotalWinners}"); } else { Console.WriteLine("Invalid input. Please enter Y or N."); } } } }
关键修复点说明
- 循环逻辑优化:输入Y时创建新
LottoGame实例,确保每次生成全新奖券,避免重复显示旧数据。 - 方法执行顺序修正:
CheckForWinner中先执行累计奖金更新再返回结果,保证奖金统计逻辑正常触发。 - 总奖金展示修正:结束游戏时直接访问
TotalWinnings属性,而非错误拼接方法引用。 - totalWinners变量落地:在
CheckForWinner中完成中奖次数统计,符合变量设计意图。
内容的提问来源于stack exchange,提问作者Deasjia Shannon
相关产品推荐
相关产品推荐

