4 Replies - 17446 Views - Last Post: 13 October 2014 - 01:11 PM Rate Topic: -----

#1 liverpooty   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 14

Random Number Guessing Game in C#

Posted 13 October 2014 - 12:50 PM

The question for homework is Create an application that generates a random number in the range of 1 through 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” If the user guesses the number, the application should congratulate the user and then generate a new random number so the game can start over"

I have no errors in my code, but when it compiles it and I type the number in, and hit calculate nothing happens at all. Im baffled. Here is my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        { }
        private void calculateButton_Click(object sender, EventArgs e)
        {
            Random random1 = new Random();
            int intRandom = random1.Next(0, 101);
            answerLabel.Text = intRandom.ToString();

            int Input = Int32.Parse(numberTextBox.Text);

            if (Input == intRandom)
                //Display the message box.
                MessageBox.Show("Congratulations!");
                //Display the message box.
            else
                MessageBox.Show("Too low, try again");

            if (Input >= intRandom)
                //Display the message box.
                MessageBox.Show("Too high, try again");



        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Close the form.
            this.Close();
        }
    }
}




Thanks in advance

This post has been edited by modi123_1: 13 October 2014 - 12:50 PM
Reason for edit:: please use the 'code' button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Random Number Guessing Game in C#

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Random Number Guessing Game in C#

Posted 13 October 2014 - 01:00 PM

You have a button1_click handler with no code.
My guess is this is what your button is wired too.

You maybe renamed the button but never redirected the event subscription.

Debugging skills are vital yet rarely taught in class. Take the time to do the debugging tutorials below and you'll be miles ahead of your school mates and waste less time on WONDERING, leaving you more time to actually LEARN.

To confirm that, put a breakpoint on line 24 of your code (as numbered here not in Visual Studio) then run. Does the breakpoint get hit?

See FAQ # 2 below

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2014
Spoiler

Was This Post Helpful? 2
  • +
  • -

#3 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 220
  • View blog
  • Posts: 691
  • Joined: 10-March 12

Re: Random Number Guessing Game in C#

Posted 13 October 2014 - 01:00 PM

You should separate the random generator in a function, so that you can reuse it after the user guesses the number. Also, your if...else it's a bit wrong. Th correct form should be:

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

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

        private int GenerateNumber()
        {
            Random random1 = new Random();
            int intRandom = random1.Next(0, 101);
            return intRandom;
        }

        private void button1_Click(object sender, EventArgs e)
        { }
        private void calculateButton_Click(object sender, EventArgs e)
        {
            int intGeneratedRandom = GenerateNumber();
            answerLabel.Text = intGeneratedRandom.ToString();

            int Input = Int32.Parse(numberTextBox.Text);

            if (Input == intGeneratedRandom)
                {
                //Display the message box.
                MessageBox.Show("Congratulations!");
                //Display the message box.
                }
            else
                {
                   if (Input < intGeneratedRandom)
                   {
                      MessageBox.Show("Too low, try again");
                   }
                   else
                   {
                      //Display the message box.
                      MessageBox.Show("Too high, try again");
                   }
                }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Close the form.
            this.Close();
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Random Number Guessing Game in C#

Posted 13 October 2014 - 01:05 PM

No. That is a very bad random function.
You are creating a new instance of the random in the method, then getting a next value. Odds are you will always get the same value. There will be very little randomness to this because the getting of the next value is so close to the creation of the Random object. Commonly a single Random object is made at class-scope level then re-used. It would also help if you seeded the Random object with some value that will be different every time... such as the CPU clock ticks value.
Was This Post Helpful? 1
  • +
  • -

#5 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 220
  • View blog
  • Posts: 691
  • Joined: 10-March 12

Re: Random Number Guessing Game in C#

Posted 13 October 2014 - 01:11 PM

Oh, i see. Never thought about that, but yet, thinking logically to your explanation, it makes sense. I also remember that last time i used Random, i've made it a class level variable. It seems i overlooked that here. I appologize.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1