我的一位教授给了我们一个创建随机数猜谜游戏的项目。对游戏的要求是至少要有一个公共属性和一个公共方法,然后才是循环的实际规则。游戏还必须至少包含两个类别。我将列出我现有的代码,希望有人可以指导我正确的方向。当它运行时,它只运行关于规则的 console.writeline 消息。然后它停止。循环不会迭代,我放在非测试类中的“方法”。从一开始我就一直在努力学习这门课,因为我以前没有编程经验。我看到的这个游戏的大多数例子只涉及一个类。所以我不完全确定我应该在另一个里面放什么,
我的代码如下,首先是非测试类。
using System;
namespace numbergame
{
class mysteryNumber
{
public int myRand {get; set;}
public int playerGuess { get; set; }
public void DisplayMessage()
{
Console.WriteLine("The most important rule of the Number Guessing Game is that \nyou can't talk about the Number Guessing Game.");
}
下一段代码是我拥有大部分代码和主要方法的地方。
using System;
namespace numbergame
{
class mysteryNumberTest
{
static void Main(string[] args)
{
int myRand;
int guessCounter = 1;
int playerGuess = 0;
Console.WriteLine("The number you must choose is a number between \n1 and 100. Your attempts remaining will be \nshown upon incorrect guess. The number of attempts it took you will be\n shown upon correct guess.");
Random randomNumbers = new Random();
myRand = randomNumbers.Next(1, 100);
{
while (guessCounter < 5) ;
Console.WriteLine("Feeling lucky, punk? Guess your number: ");
playerGuess = Convert.ToInt32(Console.ReadLine());
{
if (playerGuess == myRand)
Console.WriteLine("Congratulations! You're a real winner!\n");
Console.WriteLine("The number of attempts it took you was: {0}", guessCounter);
{
guessCounter = guessCounter + 10; }
if (playerGuess != myRand)
{ guessCounter += 1; }
Console.WriteLine("No. You have made {1} attempts.\nYou have {2} attempts left.", guessCounter, (5 - guessCounter));
if (guessCounter >= 6)
Console.WriteLine("The number of guesses was exceeded.");
}
}
}
}
}
第一个非测试类中的实例变量和自动化属性似乎毫无用处。DisplayMessage() 方法似乎也可能实际上不起作用。
但我不知道。我不确定为什么我的循环没有开始,当它开始时,我感觉我的guessCounter 可能不会按照我想要的方式递增,因为我在上周的练习中遇到了这个问题。
任何帮助表示赞赏。:(