C# 在简单的控制台游戏中跟踪积分
我很难把心思集中在这件事上。每当玩家做出错误的猜测时,它应该从初始余额中减去他/她的赌注。 因为它是在一个循环中,它总是从一开始就取得初始平衡,每次都会吐出相同的平衡。(显然) 我试过分配不同的变量,但似乎无法理解 我省略了中难度和难度的方法,因为它们现在没有用,直到我弄明白这一个 我的Main()只调用setConfession()。没有别的了C# 在简单的控制台游戏中跟踪积分,c#,console-application,C#,Console Application,我很难把心思集中在这件事上。每当玩家做出错误的猜测时,它应该从初始余额中减去他/她的赌注。 因为它是在一个循环中,它总是从一开始就取得初始平衡,每次都会吐出相同的平衡。(显然) 我试过分配不同的变量,但似乎无法理解 我省略了中难度和难度的方法,因为它们现在没有用,直到我弄明白这一个 我的Main()只调用setConfession()。没有别的了 class Control { int selectedNumber = 0; Random num = new Random();
class Control
{
int selectedNumber = 0;
Random num = new Random();
bool playAgain = true;
int difficulty = 0;
int bet = 0;
int initialBalance = 20;
int runningCredits = 0;
int credits = 0;
public Control()
{ }
//sets game difficulty
public void SetDifficulty()
{
Console.Clear();
Console.WriteLine("Please select level of difficulty between 1 - 100");
difficulty = int.Parse(Console.ReadLine());
if (difficulty >= 1 && difficulty <= 20)
LetsPlayEasy();
else if (difficulty >= 21 && difficulty <= 50)
LetsPlayMedium();
else if (difficulty >= 51 && difficulty <= 100)
LetsPlayHard();
else
{
SetDifficulty();
}
}
//easy level method
public void LetsPlayEasy()
{
//variables
int userGuess;
int numGuesses = 0;
selectedNumber = num.Next(1, 101);
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Difficulty level = EASY");
Console.WriteLine("\nBeggining credit balance = " + initialBalance);
Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!");
bet = int.Parse(Console.ReadLine());
do
{
Console.WriteLine("\nGuess a number between 1 and 100.");
userGuess = Convert.ToInt32(Console.ReadLine());
numGuesses++;
UI output = new UI();
output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses);
runningCredits = (initialBalance - bet);
Console.WriteLine("\nYou have " + runningCredits + " credits remaining.");
} while (playAgain == true);
}
类控制
{
int selectedNumber=0;
随机数=新随机数();
bool play再次=真;
智力难度=0;
int-bet=0;
初始余额=20;
int runningCredits=0;
积分=0;
公共控制()
{ }
//设置游戏难度
公共图书馆
{
Console.Clear();
Console.WriteLine(“请选择1-100之间的难度级别”);
难度=int.Parse(Console.ReadLine());
如果(难度>=1&&Demobility=21&&Demobility=51&&Demobility将runningBalance初始化为initialBalance,并仅将此值用于计算。如果只需要initialBalance一次,也可以通过简单地将runningBalance切换为initialBalance而不初始化runningBalance来完成
Console.WriteLine("\nBeggining credit balance = " + initialBalance);
Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!");
bet = int.Parse(Console.ReadLine());
runningBalance = initialBalance
do
{
Console.WriteLine("\nGuess a number between 1 and 100.");
userGuess = Convert.ToInt32(Console.ReadLine());
numGuesses++;
UI output = new UI();
output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses);
runningCredits -= bet;
Console.WriteLine("\nYou have " + runningCredits + " credits remaining.");
} while (playAgain == true);
将runningBalance初始化为initialBalance并仅将此值用于计算。如果您只需要initialBalance一次,也可以通过简单地将runningBalance切换为initialBalance而不初始化runningBalance来完成
Console.WriteLine("\nBeggining credit balance = " + initialBalance);
Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!");
bet = int.Parse(Console.ReadLine());
runningBalance = initialBalance
do
{
Console.WriteLine("\nGuess a number between 1 and 100.");
userGuess = Convert.ToInt32(Console.ReadLine());
numGuesses++;
UI output = new UI();
output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses);
runningCredits -= bet;
Console.WriteLine("\nYou have " + runningCredits + " credits remaining.");
} while (playAgain == true);
您不会更改循环中的initialBalance
或bet
,因此每个迭代都具有相同的runningCredits
值
在循环之外,执行以下操作:
runningCredits = initialBalance;
runningCredits -= bet;
在循环内部,执行以下操作:
runningCredits = initialBalance;
runningCredits -= bet;
注意:您没有任何代码来检查循环中的用户猜测是对还是错(因此,用户总是输,而您总是减去赌注)
您不会更改循环中的initialBalance
或bet
,因此每个迭代都具有相同的runningCredits
值
在循环之外,执行以下操作:
runningCredits = initialBalance;
runningCredits -= bet;
在循环内部,执行以下操作:
runningCredits = initialBalance;
runningCredits -= bet;
注意:您没有任何代码来检查循环中的用户猜测是对还是错(因此,用户总是输,而您总是减去赌注).我最初更改了它,但再次,它将重置为它最初声明的内容,因此它不起作用。比较答案的方法在另一个类中的if语句中调用…还有,-=做什么?抱歉,如果您还不知道的话,这是一个非常新的问题。同样感谢@crashmstrrunnining的帮助积分-=bet;
与runningCredits=runningCredits-bet;
相同。对于回答测试,您需要在这个级别上使用它,因此返回bool或添加另一个参考参数(首选返回值)这样你就可以知道他们是赢了还是输了,如果你从runningCredits
@MichaelM中添加或删除,那么你似乎有一个奇怪的划分,在控件中有用户输入,在用户界面中有游戏逻辑。我最初更改了它,但再次,它被重置为最初声明为是的,所以它不起作用。比较答案的方法在另一个类中的if语句中调用…还有,-=做什么?抱歉,如果你还不知道的话,这是非常新的。也谢谢你的帮助@crashmstrrunningCredits-=bet;
与runningCredits=runningCredits-bet;
一样e答案测试,您需要在这个级别使用它,因此返回bool或添加另一个参考参数(首选返回值)这样,你就可以知道他们是赢了还是输了,如果你从runningCredits
@MichaelM中添加或删除,你似乎有一个奇怪的划分,在控件中有用户输入,在UI中有游戏逻辑。