C# random number 1-100, guessing game
Last response: in Apps General Discussion
ttbspyder
October 15, 2014 4:14:08 PM
I have this code....but generating the random number is not in the right spot, can someone tell me where it goes...this runs with no errors, just doesnt run correctly
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 RandomNumberGuessingGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Welcome to the Random Number Guessing Game");
}
private void guessButton_Click(object sender, EventArgs e)
{
int number;//declare int for random number
Random rand = new Random(); //create object
number = rand.Next(101); //set int number to random number from 1-100
//user has entered guess
int guess;
guess = int.Parse(usersGuessTextBox.Text);
if (guess == number)
{
MessageBox.Show("Congratulations, you guessed it!");
}
else
{
if (guess < number) { MessageBox.Show("Too low, try again!"); }
else { MessageBox.Show("Too high, try again!"); }
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
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 RandomNumberGuessingGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Welcome to the Random Number Guessing Game");
}
private void guessButton_Click(object sender, EventArgs e)
{
int number;//declare int for random number
Random rand = new Random(); //create object
number = rand.Next(101); //set int number to random number from 1-100
//user has entered guess
int guess;
guess = int.Parse(usersGuessTextBox.Text);
if (guess == number)
{
MessageBox.Show("Congratulations, you guessed it!");
}
else
{
if (guess < number) { MessageBox.Show("Too low, try again!"); }
else { MessageBox.Show("Too high, try again!"); }
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
More about : random number 100 guessing game
-
Reply to ttbspyder
Homework assignment perhaps?
What is the expected output?
What is the actual output?
What have you tried?
What happens when you single step through the code with a watch on all variables?
Just posting a code snippet and asking what's wrong with it is not how you learn. We are not here to do your homework for you.
What is the expected output?
What is the actual output?
What have you tried?
What happens when you single step through the code with a watch on all variables?
Just posting a code snippet and asking what's wrong with it is not how you learn. We are not here to do your homework for you.
-
Reply to ex_bubblehead
m
0
l
Related resources
- Homework Help (C# GUI, Random Number Guessing Game) - Forum
- Java game on random number guessing high or low - Forum
- Evaluation of ComponentsSelected for Gaming Rig (Intel Haswell/Nvidia Maxwell Components, Init. Cost c. $1,100) - Forum
- Generating random number between -1 to 1 with C++ - Forum
ttbspyder
October 15, 2014 4:42:08 PM
When I build and run, I enter a 12 in, thinking that the number has already been generated, however when I hit the submit guess button, the form generates a new random number therefore giving false output message. And yes this is a homework assignment, but when you ask your instructor and he says, "just figure it out, you will get it" is not a learning either, I am not asking you to DO the hwk, I am simply asking for tips and suggestions, I have done most of it. Thank you! and yes I believe you are right, popatim, my bad
-
Reply to ttbspyder
m
0
l
ttbspyder
October 15, 2014 5:06:20 PM
ttbspyder
October 15, 2014 5:22:44 PM
!