如何在C#中实现赌场游戏循环运行并持续追踪资金变量?
如何在C#中实现赌场游戏循环运行并持续追踪资金变量?
嘿,作为刚入门C#的新手,你写的这个小赌场游戏已经有模有样啦!要实现循环玩游戏同时保留资金变量的需求,核心就是用循环结构把游戏的核心逻辑包起来,同时调整变量的作用域。我给你梳理下具体的优化方案:
核心思路
- 用
while循环控制游戏流程:让游戏在满足条件(比如用户还有资金,或者用户选择继续玩)时重复运行 - 调整变量作用域:把
money、Random对象这类需要跨轮次保留的变量放在循环外面 - 添加基础输入验证:避免用户输入非数字、超出范围的数值导致程序崩溃
- 优化变量命名:把
ass、shit这类不规范的变量名改成有意义的名字,提升代码可读性
修改后的完整代码
// 游戏标题 Console.WriteLine(" ___ _ "); Console.WriteLine(" / __|__ _ __(_)_ _ ___ "); Console.WriteLine("| (__/ _` (_< | ' |/ _ | "); Console.WriteLine("|_______,_/__/_|_||_|___/"); Random rnd = new Random(); int money = 10; // 初始资金,放在循环外保留值 bool isPlaying = true; // 控制游戏循环的开关 // 年龄验证只需要做一次,放在循环外 Console.WriteLine("Welcome"); Console.WriteLine("Please enter age"); int age = 0; bool validAge = false; // 验证年龄输入合法性 while (!validAge) { string ageInput = Console.ReadLine(); if (int.TryParse(ageInput, out age)) { if (age >= 18) { validAge = true; Console.WriteLine("Age verified, let's play!"); } else { Console.WriteLine("Please come back when you are 18."); return; // 年龄不够直接退出程序 } } else { Console.WriteLine("Please enter a valid number for age."); } } // 游戏主循环:只要还有资金且用户想继续就玩 while (money > 0 && isPlaying) { Console.WriteLine($"\nYou have {money} dollars."); Console.WriteLine("Please choose an integer 1-3:"); int userGuess = 0; bool validGuess = false; // 验证用户数字选择的合法性 while (!validGuess) { string guessInput = Console.ReadLine(); if (int.TryParse(guessInput, out userGuess) && userGuess >=1 && userGuess <=3) { validGuess = true; } else { Console.WriteLine("Invalid input! Please enter an integer between 1-3."); } } // 处理下注金额 int betAmount = 0; bool validBet = false; while (!validBet) { Console.WriteLine("How much would you like to bet?"); string betInput = Console.ReadLine(); if (int.TryParse(betInput, out betAmount)) { if (betAmount > 0 && betAmount <= money) { validBet = true; } else if (betAmount <=0) { Console.WriteLine("Bet must be a positive number!"); } else { Console.WriteLine($"You can't bet more than your current balance ({money} dollars)!"); } } else { Console.WriteLine("Please enter a valid number for your bet."); } } // 生成随机数(每次循环重新生成) int randomNumber = rnd.Next(1, 4); Console.WriteLine("Spinning..."); // 判断输赢并更新资金 if (userGuess == randomNumber) { int winnings = betAmount * 3; money += winnings; Console.WriteLine($"You Win! You earned {winnings} dollars."); Console.WriteLine($"Your current balance is {money} dollars."); } else { money -= betAmount; Console.WriteLine($"You Lose! You lost {betAmount} dollars."); Console.WriteLine($"Your current balance is {money} dollars."); } Console.WriteLine($"The correct number was {randomNumber}"); // 询问用户是否继续玩 if (money > 0) { Console.WriteLine("\nWould you like to play again? (Y/N)"); string continueInput = Console.ReadLine()?.Trim().ToUpper(); isPlaying = continueInput == "Y"; } else { Console.WriteLine("\nYou're out of money! Game over."); } }
关键优化点说明
- 循环结构:用外层的
while (money > 0 && isPlaying)控制整个游戏的循环,只要用户还有钱且想继续,就重复执行游戏逻辑 - 输入验证:用
int.TryParse替代Int32.Parse,避免用户输入非数字时程序崩溃;同时验证下注金额不能超过现有资金、不能为负 - 变量作用域:
money和Random对象放在循环外,确保每次循环都能保留上一次的资金值,且随机数生成更合理 - 可读性提升:把
ass、shit这类不规范变量名改成age、userGuess、betAmount,代码逻辑更清晰
你说得没错,复制粘贴代码确实是“对上帝的犯罪”,用循环才是优雅且正确的解决方式😉
备注:内容来源于stack exchange,提问作者Anderson Gleason




