1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Hey guys im new here and need some help with my coding-random guessing game

Discussion in 'C#' started by cokeiscool, Oct 23, 2009.

  1. cokeiscool

    cokeiscool New Member

    Joined:
    Oct 23, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    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);
            }
        }
    }
    
    
     
    
    This code is for a guessing game, the user inputs a number and the computer states if its right or wrong

    no I got the basics but I need to do more, I need an option to ask the user how many games he wants, and to only allow the user 6 tries before he or she loses, also I need the numbers in the range to get smaller and smaller after each guess

    im so stuck on this, any help would be great
    I tried
    Code:
    if (counter == 6)
                            Console.WriteLine("You have attempted 6 times, you lost");
                        break;
    and that didnt work

    also tried
    Code:
    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 lost.");
    
    and that didnt work either
     
  2. Todilo

    Todilo New Member

    Joined:
    Nov 14, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Do not use the continue: statement in the
    else if (input > randomNumber)
    and the
    else if (input < randomNumber)
    block

    There is here no reason to use continue since you are not trying to skip past anything. If you write your IF-statements correctly they will not collide somehow, and since you have, dont worry :D.

    When trying to solve problems like this, use the debugger, step through the program step by step F11 and you will easily find the solution.
     

Share This Page