I am fairly new to OO programming, I have created a simple "Guess the number" game, it functions correctly. However, when attempting to implement a loop errors occur. More specifically, I want the user to restart the program through a console [not compiling and running the game again].
I need help with the static void ExitGame() method. At this moment in time, I am working on "RepL.it" and the generated errors are as followed:
main.cs (10,10): warning CS0649: Field GuessTheNumber.Game.replay' is never assigned to, and will always have its default value
null'
Compilation succeeded - 1 warning (s)
Welcome to the guessing game. Please press enter.
using System;
namespace GuessTheNumber
{
class Game
{
static int UserGuess;
static int Answer;
string replay;
static string EndProg = "No";
static void Main (string[] args)
{
Console.Title = "Guess the number.";
EntryMessage();
GenerateRandom();
while (EndProg == "No") {
askData();
}
}
static void EntryMessage()
{
Console.WriteLine("Welcome to the guessing game. Please press enter.");
Console.ReadLine();
}
public static void askData()
{
while (EndProg == "No")
{
Console.WriteLine(Answer);
Console.WriteLine("Guess a number between 1 and 100.");
UserGuess = Convert.ToInt32(Console.ReadLine());
WinLose();
}
askData();
}
public void askData(bool endProg)
{
Console.WriteLine("Does you want to play again");
if (replay == "y")//; Remove this semicolon
{
Console.WriteLine("\nOkay, guess again");
askData(EndProg == "No");
}
else if (replay == "n")//; Remove this semicolon
{
askData(EndProg == "Yes");
}
else
{
Console.ReadLine();
}
}
static void GenerateRandom()
{
Random random = new Random();
Answer = random.Next(0,101);
}
static void WinLose()
{
if (UserGuess == Answer)
{
Console.WriteLine("Correct number!");
EndProg="Yes";
}
else if (UserGuess > Answer)
{
Console.WriteLine("Too high.");
EndProg="No";
}
else if (UserGuess < Answer)
{
Console.WriteLine("Too Low.");
EndProg="No";
}
else
{
Console.WriteLine("Invalid answer.");
EndProg="No";
}
}
}
}