I have a class which includes a constructor that holds hours and speed, getters and setters, and a method which calculates distace using the hours and speed variable. I need this to display a table which takes however many hours the user inputs, then for each hour display a row that shows how far per hour. here is an example:
hour distance traveled
--------------------------------
1 10
2 20
3 30
etc.
Right now my program is mostly okay, except when I run it I get someting like this:
hour distance traveled
--------------------------------
1 30
2 30
3 30
etc.
So I can tell it is a problem in my for loop but I just can't figure out what exactly I am supposed to do next to make it work. Any help is appreciated, thanks. Here is the code for my main program, I can post the class as well if needed:
import java.util.Scanner;
public class DistanceTraveledTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double testSpeed;
int testHours;
System.out.println("What is the speed of the vehicle?");
testSpeed = input.nextDouble();
while (testSpeed <= 1){
System.out.print("The speed must be equal to or greater than one");
System.out.println("What is the speed of the vehicle?");
testSpeed = input.nextDouble();
}
System.out.println("How many hours has the vehicle been travelling?");
testHours = input.nextInt();
while (testHours <= 1){
System.out.print("The hours must be equal to or greater than one");
System.out.println("How many hours has the vehicle been traveling?");
testHours = input.nextInt();
}
DistanceTraveled traveler = new DistanceTraveled(testSpeed, testHours);
System.out.println("Hour\t\tDistance Travelled");
System.out.println("------------------------------");
for(int i=0;i<=testHours;i++){
System.out.println(i + "\t\t" + traveler.findDistance());
}
}
}