# Jump/Repeat Function in C#?

## Recommended Posts

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]

##### Share on other sites
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. :)

##### Share on other sites
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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");
if (guess == Convert.ToString(num))
{
Console.Write("Correct!");
}
else
if (guess != Convert.ToString(num))
{
Console.Write("Wrong!\n");
Console.Write(Convert.ToString(num));
}
}
}

(Ignore the heading, I was messing around with other stuff too.)

##### Share on other sites
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   }}

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627714
• Total Posts
2978773

• 9
• 21
• 14
• 12
• 42