mardi 26 avril 2016

C# Generate Random number

I am wanting to generate a random number between 1 and 10. Yet I am getting a couple of errors.

 public Form1()
        {
            InitializeComponent();
        }
        int randomNumber = (0, 11);
        int attempts = 0;

    public int RandomNumber
    {
        get
        {
            return randomNumber;
        }

        set
        {
            randomNumber = value;
        }
    }

it is all on the 0, 11 under the comma is says --> struct System.Int32 represents a 32-bit signed integer <--. Under the 11 it says --> Identifier expected Syntax error, ',' expected <--. Now if I just have like int randomNumber = 0; then it will work fine, still have multiple guesses and the guess count adds up like it should, and have the too high too low labels. just the number will always be 0.

Also how can I make it to where I don't have to click the guess button, I can just hit enter on the keyboard?

private void button1_Click_1(object sender, EventArgs e)
    {
        try
        {
            if (int.Parse(textBox1.Text) > RandomNumber) label1.Text = "Too high.";

            else if (int.Parse(textBox1.Text) < RandomNumber) label1.Text = "Too low.";
            else
            {
                label1.Text = "You won.";
                textBox1.Enabled = false;
                label2.Text = "Attempts: 0";
                textBox1.Text = String.Empty;
                MessageBox.Show("You won in " + attempts + " attempts, press generate to play again.", "Winner!");
                attempts = 0;
                label2.Text = "Attempts: " + attempts.ToString();
                return;
            }
            attempts++;
            label2.Text = "Attempts: " + attempts.ToString();
        }
        catch { MessageBox.Show("Please enter a number."); }
    }




Aucun commentaire:

Enregistrer un commentaire