locked
Random number generator RRS feed

  • Question

  • Hi can anyone help?

    I have been at this for hours and I cant seem to get it. Am not that great at programming so any help would be greatly appreciated.

     

    Write a Console Application which first uses the Random Number generator to pick a number between 0 and 9 (inclusive). The program then asks a user to guess what this number is. If the user guesses correctly, the program calculates the times the user has guessed and displays the following message to the screen:

    1. a message "Well Done" in a line;

    2. a message "You have tried" followed by the number of times the user has guessed.

    Otherwise, it asks the user to guess again. This continues until the user guesses the correct number

    Friday, October 16, 2009 12:35 PM

Answers

  • You can use the help of Random Class it will generate a random number..

    System.

    Random ran = new Random();
    ran.Next(9); <-- this function return a number within maximum value 9


    :: Learning .NET ::
    Friday, October 16, 2009 12:45 PM

All replies

  • You can use the help of Random Class it will generate a random number..

    System.

    Random ran = new Random();
    ran.Next(9); <-- this function return a number within maximum value 9


    :: Learning .NET ::
    Friday, October 16, 2009 12:45 PM
  • I still dont get the layout of the code, really dont have a clue?

    Friday, October 16, 2009 12:51 PM
  • What are you having trouble with?
    Friday, October 16, 2009 12:53 PM
  • The whole question, I have no programming experience what so ever.

    Friday, October 16, 2009 12:57 PM
  • Is this for a class you are taking?
    Friday, October 16, 2009 12:58 PM
  • Yes unfortunatly, I am a networking student and there is programming in my class.

    Friday, October 16, 2009 1:01 PM
  • http://www.functionx.com/csharp/Lesson01.htm

    Going from no programming experience at all to being able to write even a simple program is going to take a few hours...
    Friday, October 16, 2009 1:04 PM
  • I dont even know why they have programming in a networking course, they are totaly different sides of computing. Thanks for the help
    Friday, October 16, 2009 1:08 PM
  • Here is sample code to help you get started this shouldn't be used for your assignment but to help you understand how this works:

    Random RandomEngine = new Random(); //Initializes the random number generator
    Int32 Number; //Variable to hold the number
    Int32 Guess = 0; //Users guess
    Int32 Guesses = 0; //Number of guesses
    Boolean Correct = false; //Variable for if they guessed correctly
    
    Number = RandomEngine.Next(0, 9); //Set the number (0-9)
    
    do //Start of a loop
    {
      Console.Clear(); //Clear the console
      Console.Write("Enter a number between 0 and 9: " ); //prompt the user for their guess.
    
      Guess = Convert.ToInt32(Console.ReadLine()); //Convert the user input to a number
    
      Guesses++; //Increment the number of guesses by 1
    
      if (Guess == Number) //Check to see if guess is correct
      {
        Correct = true;
      }
    
    } while (!Correct); //Loop until Correct = true
    
    Console.WriteLine("Well Done! You guessed the number correctly.");
    Console.WriteLine("You tried {0} time(s)", Guesses.ToString());
    Console.WriteLine("");
    Console.WriteLine("Press any key to exit...");
    Console.Read();
    
    
    

    Friday, October 16, 2009 1:10 PM
  • Random ran = new Random();
    ran.Next(9); <-- this function return a number within maximum value 9

    Check the maximum value.  It isn't 9.
    Friday, October 16, 2009 2:47 PM