Distance Traveled loop help Java

12,697

Delete the variables hour and distance.

Change the name of the loop variable from x to hour.

Simply your loop to:

for( int hour = 1; hour <= time; hour++)
    System.out.println(hour+ " " + (hour * speed));

Note that if you did used your distance variable, it was a double which is the wrong type, because:

  • both speed and time are int, so distance must be int
  • the output of a double has a decimal point, but your requirements did not show one
Share:
12,697
Pyrodemon
Author by

Pyrodemon

Updated on June 04, 2022

Comments

  • Pyrodemon
    Pyrodemon almost 2 years

    I've been racking my brain trying to figure out why my program isn't looping right. Rather than getting each hour increments I'm only getting the final hour/speed. So if I put that I'm going 40 mph for 3 hours I only get 120.

    Here is the problem:

    The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For example, if a train travels 40 miles-per-hour for three hours, the distance traveled is 120 miles. Write a program that asks for the speed of a vehicle (in mph) and the number of hours it has 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 display a report similar to the one that follows: Hour Distance Traveled

    1 40

    2 80

    3 120

    import java.util.Scanner;
    
    public class Distance{
      public static void main(String [] args){
      Scanner keyboard = new Scanner(System.in);
    
      int time , speed ,  hour;
      double  distance;
    
      System.out.println("How fast were you going ?");
      speed = keyboard.nextInt();
    
      while(speed<=0)
      {
         System.out.println("Please enter a valid speed ");
         speed = keyboard.nextInt();
      }
    
      System.out.println(" How long did you ride / drive ?");
      time = keyboard.nextInt();
    
      while(time<=0)
      {
         System.out.println("Please enter a valid time ");
         time = keyboard.nextInt();
      }
    
      System.out.println(" Hour                         Distance");
      System.out.println("---------------------------------");
      hour = 0;
    
    
      for( int x = 1; x<=time; x++)
      {
         hour++;
         if(hour>1)
         {
            distance = time * speed;
            System.out.println(time+ "                " +distance);
         }
        }
       }
      }
    
  • Pyrodemon
    Pyrodemon over 9 years
    Thanks for the help, it started at 80 which is fine I was able to fix it!
  • Pyrodemon
    Pyrodemon over 9 years
    Thanks for the help. And also thanks for pointing out my other mistakes, been beating myself up trying to figure this out all day!
  • Bohemian
    Bohemian over 9 years
    No worries. Almost always, less code is good (as long as it's readable). Less code is easier to read and debug.