Sign in to follow this  

Jump/Repeat Function in C#?

This topic is 3560 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
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

This topic is 3560 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this