fbodieslive
New member
- Joined
- Feb 15, 2015
- Messages
- 1
- Programming Experience
- Beginner
Hello,
I am a marketing major lost in a programming class. I have to code a GUI that calculates distance with a "while loop" and display the results into a listbox. I have the program working, but it isn't doing what I need it to do. When I input 40MPH into the speed textbox and 2 for hours textbox it lists 80 in the listbox. What I need it to do is list 40 then 80, to show the distance traveled the 1st hour and the 2nd hour. Here is my code.
I am a marketing major lost in a programming class. I have to code a GUI that calculates distance with a "while loop" and display the results into a listbox. I have the program working, but it isn't doing what I need it to do. When I input 40MPH into the speed textbox and 2 for hours textbox it lists 80 in the listbox. What I need it to do is list 40 then 80, to show the distance traveled the 1st hour and the 2nd hour. Here is my code.
C#:
private void calculateButton_Click(object sender, EventArgs e)
{
int speed;
int time;
int count = 1;
double distance;
if (int.TryParse(speedTextBox.Text, out speed))
{
if (int.TryParse(timeTextBox.Text, out time))
{
while (count <= time)
{
distance = (speed * time);
outputListBox.Items.Add("After hour" + count + "the distance" + "is" + distance.ToString());
count = count + 1;
}
}
else
{
MessageBox.Show("Invalid time");
}
}
else
{
MessageBox.Show("Invalid speed");
}
}