-
quick noob question
hello, i downloaded microsoft visual c# 2008 a couple days ago and i've been trying to program stuff ever since, but i'm stuck and the sites i've visited don't really answer my question.
i've tried making a game guessing game, the console generates a random number when i start the program, which is great, but when it asks me if i want to play again and i say yes, the number repeats, how can i fix that?
here's the code.
Code:
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int input;
Random number = new Random();
int n = number.Next(100);
Start:
Console.WriteLine("Guess a number between 1 and 100");
input = int.Parse(Console.ReadLine());
if (input > n)
{
goto High;
}
else
if (input < n)
{
goto Low;
}
else
if (input == n)
{
goto Right;
}
High:
Console.WriteLine("Too High");
input = int.Parse(Console.ReadLine());
if (input > n)
{
goto High;
}
else
if (input < n)
{
goto Low;
}
else
if (input == n)
{
goto Right;
}
Low:
Console.WriteLine("Too Low");
input = int.Parse(Console.ReadLine());
if (input > n)
{
goto High;
}
else
if (input < n)
{
goto Low;
}
else
if (input == n)
{
goto Right;
}
Right:
Console.WriteLine("You guessed it!");
goto New;
New:
Console.WriteLine("would you like to play again? yes/no");
string answer = Console.ReadLine();
switch (answer.ToLower())
{
case "yes":
goto Start;
case "no":
goto End;
default:
Console.WriteLine("I don't understand, ");
goto New;
}
End:
return;
}
}
}
keep in mind i only have like 10 hours of experience, on my own, so i'm really noob, thx.
-
The .Next is what gets you a new random number, so you should be sure that when you re-start that line happens.
(And after you get that working, you can start looking at loops and if.)
-
haha sweet.
i got it to work, all i had to do was to put the label over the random generating code and not below, like it was.
sweet, thx for the tip, i'll get to loops now.
i think i'm learning a lot if i find things out myself, i'll be doing a course at college in january, but i want to know some stuff beforehand, which is why i'm messing around now.