I have recently switched from Java to C# due to their similarity. I have made a guessing game console application for C#. The computer picks a random number between 1 - 10 and the user has three attempts to guess the number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class WinnerCloser
{
public WinnerCloser()
{
int x = 0;
x++;
if ((x > 0))
Console.WriteLine("\r\nPress any key to exit...");
Console.ReadKey();
Environment.Exit(0);
}
}
class LoserCloser
{
public LoserCloser()
{
Console.WriteLine("\r\nSorry, that's three guesses!");
}
}
class Program
{
static void Main(string[] args)
{
Random r = new Random();
int pickNum = r.Next(11);
Console.WriteLine("Welcome to The Number Game!");
Console.WriteLine("\r\nThe purpose of this game is to guess the number the computer has picked.");
Console.WriteLine("The number is between 1 and 10. You will have three attempts to guess\r\nthe right number. Good luck!");
Console.WriteLine("\r\nEnter your first guess:");
Console.WriteLine("\r\n");
String firstGuess = Console.ReadLine();
int guess1 = Int32.Parse(firstGuess);
if ((guess1 == pickNum))
{
Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");
new WinnerCloser();
}
else
Console.WriteLine("\r\nSorry, guess again!");
Console.WriteLine("\r\nEnter your second guess:");
Console.WriteLine("\r\n");
String secondGuess = Console.ReadLine();
int guess2 = Int32.Parse(secondGuess);
if ((guess2 == pickNum))
{
Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");
new WinnerCloser();
}
else
Console.WriteLine("\r\nSorry, guess again!");
Console.WriteLine("\r\nEnter your third guess:");
Console.WriteLine("\r\n");
String thirdGuess = Console.ReadLine();
int guess3 = Int32.Parse(thirdGuess);
if ((guess3 == pickNum))
{
Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");
new WinnerCloser();
}
else
{
new LoserCloser();
Console.WriteLine("\r\nThe number was " + pickNum + "!");
Console.WriteLine("\r\nPress any key to exit..");
Console.ReadKey();
}
}
}
}
I am creating the classes WinnerCloser and LoserCloser because I cannot get them to be methods elsewhere. This is the only way I have been able to have them in the program and use them without the compiler giving me an error. I tried declaring the methods before the class program, but that didn't work either. Suggestions would be appreciated.
Thanks in advanced :thumbup1:


Sign In
Create Account


Back to top









