Jump/Repeat Function in C#?

Started by
2 comments, last by abso 16 years, 1 month ago
I am trying to make a random number guessing game. The only thing I can't figure out is how to make it repeat/restart/jump to certain areas of the code. I have tried a 'while' loop, it just repeats what I told it to repeat for wrong. Is there a function or a way I could do this? (I am sure, it is something obvious and I don't realize it. :P) [Edited by - Mashew on March 19, 2008 4:48:17 PM]
Advertisement
Usually a while loop or a do-while loop will work for this type of situation.

Post your code. Maybe there is a hard-to-spot typo. :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

class Program
{
static void Main(string[] args)
{
string guess;
Console.Write("Welcome to Guess the Number Game! Version 1!\n");
Random Random = new Random();
int num = Random.Next(9);
Console.Write("Now guess the number!!! :D (HINT: Higher than 0, Lower than 10!)\n");
guess = Console.ReadLine();
if (guess == Convert.ToString(num))
{
Console.Write("Correct!");
Console.Read();
}
else
if (guess != Convert.ToString(num))
{
Console.Write("Wrong!\n");
Console.Write(Convert.ToString(num));
Console.Read();
}
}
}


(Ignore the heading, I was messing around with other stuff too.)
fyi, if you put your code in "["code"]" "["/code"]" tags (just remove the quotes, had to put them in there to make the brackets show up), it will preserve the formatting and make it much easier for us to read. =P

Normally you would do something like this to loop over the type of game logic you want to do:

quit = false;while (quit == false) or some other end condition not met{   do some game logic to set up this turn, ie random number = some new random number   output something to screen: "Guess a number! Type "Quit" to exit"   get user input for what they think the number is   if the user typed quit   {      quit = true;   }   else   {     check if the user's input matches the value your game logic came up with     do whatever else you want to do if it matches or doesn't match   }}

This topic is closed to new replies.

Advertisement