Another problem is I'm getting a message saying targetNum doesn't exist in the current context. I've seen code like this and mine seems the same, but gets that error. Any help is appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace GuessingGameCh8
{
public partial class Form1 : Form
{
int numOfTries = 0;
int guessNum;
//int targetNum;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
int targetNum = r.Next(0, 100);
}
private void guessBtn_Click(object sender, EventArgs e)
{
guessNum = int.Parse((txtGuess.Text));
//Random r = new Random();
//int targetNum = r.Next(0, 100);
//while(numOfTries < 20)
//{
if (guessNum < targetNum)
{
lblGuess.Text = "Your number is too low!";
this.BackColor = Color.Blue;
txtGuess.Clear();
numOfTries++;
//break;
}
else if (guessNum > targetNum)
{
lblGuess.Text = "Your number is too high!";
this.BackColor = Color.Red;
txtGuess.Clear();
numOfTries++;
//break;
}
else if (guessNum == targetNum)
{
lblGuess.Text = "You've Guessed Right!";
numOfTries++;
txtGuess.Clear();
MessageBox.Show("Number of tries:" + numOfTries);
//break;
}
//}
}
}
}

New Topic/Question
Reply




MultiQuote




|