Gingy
Guessing game
Have several build errors for a guessing game i made in a form app
using Guessing_Game;
using System;
using System.Windows.Forms;
namespace GuessingGame
{
public partial class Form1 : Form
{
private int numberToGuess;
private int attempts;
public Form1()
{
InitializeComponent();
StartNewGame();
}
private void StartNewGame()
{
numberToGuess = new Random().Next(1, 11); // Random number between 1 and 10
attempts = 0;
lblFeedback.Text = "I'm thinking of a number between 1 and 10. Can you guess it?";
txtGuess.Clear();
txtGuess.Enabled = true;
btnCheckGuess.Enabled = true;
btnPlayAgain.Visible = false;
}
private void btnCheckGuess_Click(object sender, EventArgs e)
{
if (int.TryParse(txtGuess.Text, out int guess))
{
attempts++;
if (guess < 1 || guess > 10)
{
lblFeedback.Text = "Please guess a number between 1 and 10.";
}
else if (guess < numberToGuess)
{
lblFeedback.Text = "Your guess is too low. Try again!";
}
else if (guess > numberToGuess)
{
lblFeedback.Text = "Your guess is too high. Try again!";
}
else
{
lblFeedback.Text = $"Congratulations! You've guessed the number {numberToGuess} correctly in {attempts} attempts.";
txtGuess.Enabled = false;
btnCheckGuess.Enabled = false;
btnPlayAgain.Visible = true;
}
}
else
{
lblFeedback.Text = "Invalid input! Please enter a number.";
}
}
private void btnPlayAgain_Click(object sender, EventArgs e)
{
StartNewGame();
}
}
}
3 replies
learning C#
having trouble with a simple code asking for name and age and the app should output it but doesnt seem to be working any help is greatly appreciated
namespace collectNameAgeApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
int age;
if (int.TryParse(textBox2.Text, out age))
{
label3.Text = $"Hello {name}, you are {age} years old.";
}
else
{
label3.Text = "Please enter a valid age.";
}
}
}
}
26 replies