Jump to content
  • Advertisement
Sign in to follow this  
Mashew

Jump/Repeat Function in C#?

This topic is 3772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
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
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!