# Distance traveled problem

• June 25th, 2013, 08:53 AM
Steph2752
Distance traveled problem
I am extremely new to java and having a hard time grasping it for my summer class. I am stuck on one problem so if anybody could help that would be great!

The distance a vehicle travels can be calculated as follows:
Distance = Speed * Time

Write a program that asks the user for the speed of a vehicle (in MPH) and the number of hours they traveled. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. For example, if a vehicle is traveling at 40 MPH for a three hour time period, it should show something like this:

Hour Distance Traveled
-----------------------------------
1 40
2 80
3 120

Input Validation: Do not accept a negative number for speed and do not acept any value less than 1 for time traveled

This is what I Have so far...

package lab4;

public class num2
{
public static void main(String[] args)
{

final int Time_Incrememnt = 1;

int d;
int s;
int t;
int i;
String input;

System.out.println("What is the speed of the vehicle in MPH?");

s = keyboard.nextInt();

while (s <= 0)
{

}

System.out.print("Enter the number of hours that you have traveled: ");

t = keyboard.nextInt();

while (t <= 1)
{

System.out.println("Hours Distance Traveled");
System.out.println("--------------------------------");

for (i = 1; i <= t; i++)

d = s*t;

}
}

}
• June 25th, 2013, 08:55 AM
KevinWorkman
Re: Distance traveled problem
When posting code, please use the highlight tags to preserve formatting.

What does that code do? Does it compile? Does it run? Does it do what you expect? If not, where does its behavior differ from what you want? Where exactly are you stuck?
• June 25th, 2013, 09:00 AM
Steph2752
Re: Distance traveled problem
That one is not finished at the end. I've had trouble getting it to correctly compute the distance traveled and get the hours as displayed in the example. It asks the right questions and everything I just can't get the final chart
• June 25th, 2013, 10:43 AM
KevinWorkman
Re: Distance traveled problem
Well, time to debug your program. What happened when you stepped through it with a debugger, or at least added some print statements to figure out what's going on? What happens on each line?