C# 我在骰子游戏中遇到问题,每次用户玩游戏时都需要增加一个值,然后将其打印回给他们



我有一个骰子游戏,它有两个骰子,发生的事情是,一个游戏将通过,然后询问用户是否要再次玩。例如,如果他们说了三次"是",那么当他们最后说"不"退出游戏时,他们会得到一个输出,告诉他们他们玩了多少次游戏。我在想出它的代码时遇到了麻烦。

我没有太多使用参数和返回类型的经验(这是一个初学者作业),但我目前有一个添加 1 的计数器。问题是它从 0 开始,到 1,然后停留在那里。

这是我在游戏中运行的代码:

namespace MajorAssignment1
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Ray's Dice Game! Let's Start");
Console.WriteLine();
EvenOrOdds();
}
public static void EvenOrOdds()
{
Random rnd = new Random();
int die1 = rnd.Next(1, 10);
int die2 = rnd.Next(1, 10);
Console.WriteLine("Die 1 = {0} and Die 2 = {1}", die1, die2);
Console.WriteLine();
Console.WriteLine("You Rolled {0} and {1}", die1, die2);
Console.WriteLine();
if ((die1 + die2) % 2 == 0)
{
Console.WriteLine("Evens are better than odd.");
Console.WriteLine();
}
if ((die1 + die2) % 2 > 0 )
{
Console.WriteLine("Odds are still cool.");
Console.WriteLine();
}

Console.WriteLine("Do you want to play again? Please enter in all caps YES or NO");
String UserInput = Console.ReadLine();
var Counter = 0;
if (UserInput == "YES")
{
EvenOrOdds();
Counter = Counter + 1;
}
else
{
Console.WriteLine("The number of times the dice was thrown is:" + Counter);
Console.WriteLine();
}

}
public static void Outro()
{ 
Console.WriteLine("Thanks for playing! Come again!");
Console.WriteLine();
}
}
}

通过从自身内部调用 EvenOrOdds() 以"再次播放",您实际上是在创建对该函数的递归调用。

您在调用的每个 EvenOrOdds() 实例的范围内重新定义计数器,导致计数器始终以 1 结尾。

一个简单的选择是将 Counter 的定义移到类级变量中,这意味着它将在你对 EvenOrOdds() 的所有调用之间共享

class MainClass
{
//class-level static variable
private static int Counter;
public static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Ray's Dice Game! Let's Start");
Console.WriteLine();
EvenOrOdds();
}
// rest of code here

这意味着您可以在 EvenOrOdds() 代码中删除计数器的定义。现在,当您递增计数器时,它正在更新类级变量,这将导致您预期的计数器行为。

Console.WriteLine("Do you want to play again? Please enter in all caps YES or NO");
String UserInput = Console.ReadLine();
if (UserInput == "YES")
{
//increment the counter first
Counter = Counter + 1;
//then call the method again for a new game
EvenOrOdds();
}

您也可以更改"计数器 = 计数器 + 1;"行,您使用内联 ++ 增量运算符:"计数器++;",这将执行相同的操作。

注意:还有其他方法可以实现这种类型的"再次播放"功能会更好,例如使用循环等,但无需重写您已经完成的工作,我上面的建议就足以作为实现您想要做的微小更改。 祝你好运!

编辑:更新为在再次调用EventOrOdds()之前先增加计数器 - 这导致计数器在每场比赛中正确递增。

代码的问题在于您以递归方式调用EvenOrOdds(),并且counter永远不会递增。而且,你正在以复杂的方式做简单的事情,我简化了一些事情。

工作代码:

using System;
public class diceCounter
{
public static void Main(string[] args)
{
String UserInput;
int Counter =1;
Console.WriteLine("Hey! Welcome to Ray's Dice Game! Let's Start");
Console.WriteLine();
do
{
EvenOrOdds();
Console.WriteLine("Do you want to play again? Please enter in all caps YES or NO");
UserInput = Console.ReadLine();
if (UserInput.Equals("YES"))
{
Counter++;
EvenOrOdds();
}
}while(!(UserInput.Equals("NO")));
Console.WriteLine("The number of times the dice was thrown is: " + Counter);
Outro();
}
public static void EvenOrOdds()
{
Random rnd = new Random();
int die1 = rnd.Next(1, 10);
int die2 = rnd.Next(1, 10);
Console.WriteLine("Die 1 = {0} and Die 2 = {1}", die1, die2);
Console.WriteLine();
Console.WriteLine("You Rolled {0} and {1}", die1, die2);
Console.WriteLine();
if ((die1 + die2) % 2 == 0)
{
Console.WriteLine("Evens are better than odd.");
Console.WriteLine();
}
if ((die1 + die2) % 2 > 0 )
{
Console.WriteLine("Odds are still cool.");
Console.WriteLine();
}
}
public static void Outro()
{ 
Console.WriteLine("nThanks for playing! Come again!n");
}
}

我可以想出两个解决方案

1) 在类级别使用私有变量。请记住在您的方法中删除计数器的定义

class MainClass {
private static int Counter = 0;
...
}

2) 将 ref 参数发送到您的方法

public static void EvenOrOdds(ref int counter)

和在主要 事件或赔率(计数器)。递归也是如此

最新更新