Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7504053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:26:27+00:00 2026-05-29T21:26:27+00:00

I need a little help with a Random Number Guessing Game in visual studio.

  • 0

I need a little help with a Random Number Guessing Game in visual studio. I got the brunt of the code down but I am having troubles with the Random number generator and getting the random number to port into the click events. As always, I don’t really need code but some guidance and/or explanations as to what I am doing wrong and if there is a more effecient way to do things in the beginner phases of learning. Below is my code, the comments are the parts where I am having troubles. Thanks for any help as the help I’ve recieved to date as been phenominal.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LAB6B
{
    public partial class game : Form
    {
        public game()
        {
            InitializeComponent();

            //Generate Random number between 1 and 100
         //Not sure if there is a better way?
            Random rand1 = new Random();
            int num1 = rand1.Next(1,50);
            int num2 = rand1.Next(1,50);
            int answer = num1 + num2;

        }

        private void evaluate_Click(object sender, EventArgs e)
        {
            int count = 0;
            int choice = Convert.ToInt32(guess);


            if (guess.Text != string.Empty)
            {
                // set counter to keep track of how many tries
                // should this be done by a loop or will it count without a loop?
                count++;

                //compare user input against random number
          //Can’t import the random number for comparision
                if (choice < answer)
                {
                    Evaluate.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Too Low!";
                    Clear.Visible = true;
                    BackColor = Color.LightSeaGreen;
                }
                else if (choice > answer)
                {
                    Evaluate.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Too High!";
                    Clear.Visible = true;
                    BackColor = Color.SlateBlue;
                }
                else
                {
                    //Display correct message along with how many times it took to get it
                    MessageBox.Show(" Eso es CORRECTO! It took you {0} tries. ", count);
                }
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            guess.Text = "";
            Evaluate.Visible = true;
            lblMessage.Visible = false;
            Clear.Visible = false;
            BackColor = Color.PowderBlue;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:26:30+00:00Added an answer on May 29, 2026 at 9:26 pm

    As the rand1 and answer variables are defined within the constructor, you can only access them in the constructor. Defining answer on the class level will solve most of the problems, as you will be able to access it both from the constructor and the click handlers, like this:

    private int answer;
    private int count;
    
    public game()
    {
      InitializeComponent();
    
      //Generate Random number between 1 and 100
      Random random= new Random();
      // no need for num1 and num2, it's just as random
      answer = random.Next(1,101);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java: generating random number in a range I need a little help.
I need little help with inheritance in C++. I have code with same structure
I need a little help here. This code correctly displays every format I enter--except
I need a little help with Math.random() : I have to rotate some images
I need a little help if someone's got a minute. I've written a web
I'm just figuring out my way around rails but I need a little help
Need a little help with a SQL / ActiveRecord query. Let's say I have
I need a little help on this subject. I have a Web application written
I need a little help getting a tar file to download from a website.
I need a little help creating a catch-all error handling page in my ICEfaces

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.