我有一个骰子游戏,持有两个骰子,会发生的是一个游戏将通过,然后将询问用户是否要再次玩。例如,如果他们三次说是,那么当他们最后说不退出游戏时,他们会得到一个输出,告诉他们他们玩了多少次游戏。我无法想出它的代码。
我没有太多使用参数和返回类型的经验(这是一个初学者的分配)但我目前有一个增加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()
,而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");
}
}
通过从内部调用EvenOrOdds()以“再次播放”,您实际上是在创建对函数的递归调用。
您正在调用EvenOrOdds()的每个实例的范围内重新定义Counter,导致Counter总是以1结尾。
一个简单的选择是将Counter out的定义移到类级变量中,这意味着它将在所有对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()代码中删除Counter的定义。现在,当您递增Counter时,它正在更新类级变量,这将导致您的预期Counter行为。
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();
}
你也可以改变“Counter = Counter + 1;”你使用内联++增量运算符的行:“Counter ++;”这将做同样的事情。
注意:还有其他方法可以实现这种类型的“再次播放”功能,这种功能会更好,例如使用循环等,但是如果不重写已经完成的工作,我上面的建议就足以作为实现你想要的小改变了去做。祝好运!
编辑:更新以在再次调用EventOrOdds()之前首先递增计数器 - 这会导致计数器在每次游戏中正确递增。
我可以提出两个解决方案
1)在类级别使用私有变量。请记住在您的方法中删除计数器的定义
class MainClass {
private static int Counter = 0;
...
}
2)向您的方法发送ref参数
public static void EvenOrOdds(ref int counter)
并在主EventOrOdds(柜台)。你的递归也一样