i need help modifying the program so that the use must enter a number between 0
ID: 3544179 • Letter: I
Question
i need help modifying the program so that the use must enter a number between 0 and 30 inclusive for the number of contestant each year. and if the user enters an incorrcet number, the program prompts for a valid value.
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 WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int numberOfContestantsLastYear;
int numberOfContestantsThisYear;
int expectedRevenue;
int difference;
numberOfContestantsLastYear = Convert.ToInt32(textBox1.Text);
numberOfContestantsThisYear = Convert.ToInt32(textBox2.Text);
expectedRevenue = numberOfContestantsThisYear * 25;
difference = numberOfContestantsThisYear - numberOfContestantsLastYear;
label3.Text = "The Number of contestants in last year's competition was :" + numberOfContestantsLastYear;
label4.Text = "The Number of contestants expected in this year's competition is :" + numberOfContestantsThisYear;
label5.Text = "We expect that this year we will have a revenue of: $" + expectedRevenue;
if (numberOfContestantsThisYear > numberOfContestantsLastYear)
{
label6.Text = "This year has more contestants than the previous year, the number increased by: " + difference;
}
else if (numberOfContestantsLastYear > numberOfContestantsThisYear)
{
label6.Text = "Last year there were more contestants than this year, the number decreased by: " + difference;
}
else
{
label6.Text = "This year's number of contestants is the same as the previous year's";
}
if (numberOfContestantsThisYear <= numberOfContestantsLastYear)
{
label6.Text = String.Format ("A tighter race this year! Come out and case your vote!");
}
else if (numberOfContestantsThisYear > numberOfContestantsLastYear && numberOfContestantsThisYear <= numberOfContestantsLastYear *2)
{
label6.Text = String.Format ("The competition is bigger than ever!");
}
else
{
label6.Text = String.Format("The competition is more than twice as big this year!");
}
}
private void Form1_Load (object sender, EventArgs e)
{
}
}
}
Explanation / Answer
numberOfContestantsLastYear = Convert.ToInt32(textBox1.Text);
numberOfContestantsThisYear = Convert.ToInt32(textBox2.Text);
You can check for condition after them
as
if (numberOfContestantsThisYear>30 || numberOfContestantsThisYear <0)
MessageBox.show("you have entered wrong no. of contestents");
if (numberOfContestantslastYear>30 || numberOfContestantslastYear <0)
MessageBox.show("you have entered wrong no. of contestents");
Also add a check bool in if & else loop
if no are wrong make that variable false
Result should show up only when that variabe is true;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.