1

Hi so I am new to programing I just started school and I wanted to get a head start on programing so please keep in mind that everything I show you is all self-taught. Here is my question I wanted to make a random number guessing game and for the most part it works but every time you click the button to guess it randoms a different number which I don’t want here is what I have so far

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    // number of guesses
    int numberOfGesses = 0;

    private void btnCalc_Click(object sender, EventArgs e)
    {
        // make the generator
        Random generator = new Random();

        //make the number
        int number = generator.Next(1, 10);


        // get the users guess
        int guess = int.Parse(txtInput.Text);

        //check the users guess
        if (guess == number)
        {
            lblAnswer.Text = "You got it";
            numberOfGesses = 0;
        }
        else if (guess != number)
        {
            numberOfGesses = numberOfGesses + 1;
            lblAnswer.Text = "try agian you have gessed" + (numberOfGesses) + " times";
        }

    }


}

I know it keeps creating a new number because every time I press the guess button it starts from the top and makes a new number. I tried to take this block and make it global but I got an error

// make the generator
        Random generator = new Random();

        //make the number
        int number = generator.Next(1, 10);

again im realy new and i found this site when lookinging up some qeustions i had so i thought it would be a good place to help me learn about programing while i wait till i can get into the programing classes thank you for your time.

2 Answers2

1

You likely got an error because C# doesn't allow you to assign a default value of a field based on another field.

public partial class Form1 : Form {
    int numberOfGuess = 0;
    Random generator = new Random();
    int number;

    // other methods
}

generator can be initialized before or after number, hence the error. Instead, you can put it in the form intializer (Form1 method), or make another button and click it and generate a new random number:

public partial class Form1 : Form
{
    // number of guesses
    int numberOfGesses = 0;
    Random generator = new Random();
    int number;

    public Form1()
    {
        InitializeComponent();

        // Generate the random number
        number = generator.Next(1, 10);
    }

    private void btnRandom_Click(object sender, EventArgs e)
    {
        // Generate a new random number when you click a button on the form
        number = generator.Next(1, 10);
    }

    private void btnCalc_Click(object sender, EventArgs e)
    {
        // get the users guess
        int guess = int.Parse(txtInput.Text);

        //check the users guess
        if (guess == number)
        {
            lblAnswer.Text = "You got it";
            numberOfGesses = 0;
        }
        else if (guess != number)
        {
            numberOfGesses = numberOfGesses + 1;
            lblAnswer.Text = "try agian you have gessed" + (numberOfGesses) + " times";
        }
    }
}
Code Different
  • 73,850
  • 14
  • 125
  • 146
  • That worked I just want to make sure i understand this does the initialize component block just what the program does before it boots the user interface? – Matthew Schutten Oct 07 '15 at 03:22
  • also when i mouse over the initialize component it says it is required method for designer support and i should not modify it. but didnt i just modify the code? – Matthew Schutten Oct 07 '15 at 03:25
  • The `Form1` method is invoked when the form starts. It's called the initializer or the constructor. `InitializeComponent` set up the controls on your form. Best to leave it alone. If you want more detail, read this question: http://stackoverflow.com/questions/12297079/very-simple-definition-of-initializecomponent-method – Code Different Oct 07 '15 at 03:26
0

You cannot use an instance variable to initialize another instance variable. Why? Because the compiler can rearrange these - there is no guarantee that generator will be initialized before number, so the above line might throw a NullReferenceException. So change the default number value to 0:

Random generator = new Random();
int number = 0;

Initialise in the constructor:

 public Form1()
    {
        InitializeComponent();
        number = generator.Next(1, 10);
    }

When the button is clicked generate the number since here you will be needing it:

 private void btnCalc_Click(object sender, EventArgs e)
    {
                //take the input & Compare as before.
    }
Nikita Shrivastava
  • 2,778
  • 7
  • 18