How do I put a loop inside another loop?

I have to set rules to my random guessing game and im kinda stuck

Page 1 of 1

4 Replies - 1680 Views - Last Post: 24 October 2009 - 09:20 AM Rate Topic: -----

#1 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

How do I put a loop inside another loop?

Post icon  Posted 12 October 2009 - 10:39 AM

Ok so I have to create a game where the user guesses what random number the computer has created

there are some rules I have to put into the game and am having a little trouble, I have created the basic set up of the game, the user picks a number between 1-20 if he/she picks too high or too low, it says too high or too low

my problem is I have to set up a number specific loop where the user gets to choose how many games he wants to play, would I have to put another while loop?

also I have to get the range to become more specific for example, if the user picks 7 and it is too high the response next states please pick a number between 1-6, and if they pick 2 and its too low, then it says please pick 3-6

the inputs I keep putting are too static and of course dont change, is that another loop I have to do

and last it has to stop after 6 tries and state the user has lost after 6 tries

I tried an if statement

I realize that if even that if statement worked it would just exit which I dont want it too, would this be another loop too and would it be a while loop state, or could I use a switch case or am I completely off there
 if (counter == 6)
						Console.WriteLine("You have attempted 6 times, you lost");
					break;


code of what I do have
using System;
using System.Collections.Generic;
using System.Text;

namespace _._
{
	class Program
	{
		static void Main(string[] args)
		{
			while (true)
			{
				int randomNumber = GenerateNumber(1, 21);
				int counter = 1;
				while (true)
				{
					Console.Write("Enter a number between 1 and 20(0 to quit):");
					int input = Convert.ToInt32(Console.ReadLine());

					if (input == 0)
						return;
					else if (input < randomNumber)
					{
						Console.WriteLine("Too low, try again.");
						++counter;
						continue;
					}
					else if (input > randomNumber)
					{
						Console.WriteLine("Too high, try again.");
						++counter;
						continue;
					}
					else
					{
						Console.WriteLine("You guessed it! The number was {0}!", randomNumber);
						if (counter == 1)
							Console.WriteLine("And you guessed it right away! Bonus points!\n");
						else
							Console.WriteLine("It took you {0} {1}.\n", counter, counter == 1 ? "try" : "tries");
						break;
					}
				}
			}

		}
		static int GenerateNumber(int min, int max)
		{
			Random random = new Random();
			return random.Next(min, max);
		}
	}
}


 


I actually did try this one and have the bulk of it done, just these little extra parts im a little confused about, I dont want the answer just a little push in the right direction

This post has been edited by jmartin51: 12 October 2009 - 10:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I put a loop inside another loop?

#2 Toolsmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-December 07

Re: How do I put a loop inside another loop?

Posted 12 October 2009 - 11:11 AM

View Postjmartin51, on 12 Oct, 2009 - 09:39 AM, said:

Ok so I have to create a game where the user guesses what random number the computer has created

there are some rules I have to put into the game and am having a little trouble, I have created the basic set up of the game, the user picks a number between 1-20 if he/she picks too high or too low, it says too high or too low

my problem is I have to set up a number specific loop where the user gets to choose how many games he wants to play, would I have to put another while loop?

also I have to get the range to become more specific for example, if the user picks 7 and it is too high the response next states please pick a number between 1-6, and if they pick 2 and its too low, then it says please pick 3-6

the inputs I keep putting are too static and of course dont change, is that another loop I have to do

and last it has to stop after 6 tries and state the user has lost after 6 tries

I tried an if statement

I realize that if even that if statement worked it would just exit which I dont want it too, would this be another loop too and would it be a while loop state, or could I use a switch case or am I completely off there
 if (counter == 6)
						Console.WriteLine("You have attempted 6 times, you lost");
					break;


code of what I do have
using System;
using System.Collections.Generic;
using System.Text;

namespace _._
{
	class Program
	{
		static void Main(string[] args)
		{
			while (true)
			{
				int randomNumber = GenerateNumber(1, 21);
				int counter = 1;
				while (true)
				{
					Console.Write("Enter a number between 1 and 20(0 to quit):");
					int input = Convert.ToInt32(Console.ReadLine());

					if (input == 0)
						return;
					else if (input < randomNumber)
					{
						Console.WriteLine("Too low, try again.");
						++counter;
						continue;
					}
					else if (input > randomNumber)
					{
						Console.WriteLine("Too high, try again.");
						++counter;
						continue;
					}
					else
					{
						Console.WriteLine("You guessed it! The number was {0}!", randomNumber);
						if (counter == 1)
							Console.WriteLine("And you guessed it right away! Bonus points!\n");
						else
							Console.WriteLine("It took you {0} {1}.\n", counter, counter == 1 ? "try" : "tries");
						break;
					}
				}
			}

		}
		static int GenerateNumber(int min, int max)
		{
			Random random = new Random();
			return random.Next(min, max);
		}
	}
}


 


I actually did try this one and have the bulk of it done, just these little extra parts im a little confused about, I dont want the answer just a little push in the right direction




Since you have a maximum number of guesses, why not put that as the outer loop? That way could, if you wanted, make it configurable.

	 .
	 .
	 .
	 int guess = 0;
	 for( guess = 0; guess < 6; guess++ )
	 {
		   .
		   .
		   .
		   if( input == random )
			 break;
	 }
	 if( guess < 6 )
		Console.WriteLine("It took you {0} {1}.\n", guess+1, (guess + 1) == 1 ? "try" : "tries");
	 else
		Console.WriteLine("You blew.");



** Note that you need the counter variable defined OUTSIDE the for loop so that it's value can be checked after the loop ends.

This post has been edited by Toolsmith: 12 October 2009 - 11:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

Re: How do I put a loop inside another loop?

Posted 12 October 2009 - 11:26 AM

where exactly do I put that code, iv tried before and after the loop and it doesnt change anything, after 6 tries it doesnt say anything
Was This Post Helpful? 0
  • +
  • -

#4 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

Re: How do I put a loop inside another loop?

Posted 23 October 2009 - 12:12 PM

I am still pretty stuck, I dont know how to write it to say

if input = such and such number, the game will play such and such time

and I cant get the 6 try limit to get instated

using System;
using System.Collections.Generic;
using System.Text;

namespace GuessTheNumberGame
{
	class Program
	{
		static void Main(string[] args)
		{
			while (true)
			{
				int randomNumber = GenerateNumber(1, 21);
				int counter = 1;

				int guess = 0;
				
			   
				
				while (true)
				{
					Console.Write("Enter a number between 1 and 20(0 to quit):");
					int input = Convert.ToInt32(Console.ReadLine());

					if (input == 0)
						return;
					else if (input < randomNumber)
					{
						Console.WriteLine("Too low, try again.");
						++counter;
						continue;
					}
					else if (input > randomNumber)
					{
						Console.WriteLine("Too high, try again.");
						++counter;
						continue;
					}
					for (guess = 0; guess < 6; guess++)
					{
						if (input == randomNumber)
							break;


						if (guess < 6)
							Console.WriteLine("It took you {0} {1}.\n", guess + 1, (guess + 1) == 1 ? "try" : "tries");
						else
							Console.WriteLine("You blew.");

					}
				
					
				   
				}
			}

		}
		static int GenerateNumber(int min, int max)
		{
			Random random = new Random();
			return random.Next(min, max);
		}
	}
}






using System;
using System.Collections.Generic;
using System.Text;

namespace NumberGuessingGame
{
	class Program
	{
		static void Main(string[] args)
		{
			int numberOfGames;
			numberOfGames = GetNumberOfGames();
			int i = 0;

			for (int i = 0; i < numberOfGames; i++)
			{
				if (input == i)
				   
			}  // for i (repeat number of games)

			// hold screen
			Console.ReadLine();
		}


		public static int GetNumberOfGames()
		{
			// get user to choose number of games
			string inputString = "";
			int numGames = 0;

			Console.Write("Enter number of games you'd like to play: ");
			inputString = Console.ReadLine();
			numGames = Convert.ToInt32(inputString);

			return numGames;
		}  // method GetNumberOfGames

		public static int GetRandomNumber()
		{
			Random numb = new Random();
			int s = numb.Next(20);  // produces positive number  < 20
			s += 1;   // randome number is 1 to 20 (not 0 to 19)
			return s;
		}  // method GetRandomNumber

	}
}




I have these two codes and still no idea what im doing, haha
Was This Post Helpful? 0
  • +
  • -

#5 LetMeFinclOut  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 115
  • Joined: 14-May 09

Re: How do I put a loop inside another loop?

Posted 24 October 2009 - 09:20 AM

You shouldn't need an if statement, since the for loop does the work for you.

using System;
using System.Collections.Generic;
using System.Text;

namespace NumberGuessingGame
{
	class Program
	{
		static void Main(string[] args)
		{
			int numberOfGames;
			numberOfGames = GetNumberOfGames();

			//  'initial value	   'condition to continue loop   'value updated at end of each loop
			for (int currentGame = 1; currentGame <= numberOfGames; currentGame++)
			{
				Console.WriteLine("Beginning game {0}.\n", currentGame)

				//GetRandomNumber
				//begin guessing loop
				//etc			   
			}

			// hold screen
			Console.ReadLine();
		}


		public static int GetNumberOfGames()
		{
			// get user to choose number of games
			string inputString = "";
			int numGames = 0;

			Console.Write("Enter number of games you'd like to play: ");
			inputString = Console.ReadLine();
			numGames = Convert.ToInt32(inputString);

			return numGames;
		}  // method GetNumberOfGames

		public static int GetRandomNumber()
		{
			Random numb = new Random();
			int s = numb.Next(20);  // produces positive number  < 20
			s += 1;   // randome number is 1 to 20 (not 0 to 19)
			return s;
		}  // method GetRandomNumber

	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1