Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Distance Traveled problem help

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Distance Traveled problem help

    My code compiles, but when running it, it continues to infinity. How do I change that?

    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

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

    Here is my code:
     import java.util.Scanner;
     
    public class DistanceTraveled
    {
       public static void main(String[] args)
       {
          Scanner keyboard = new Scanner(System.in);
          int vehicleSpeed = 0, hoursTraveled = 0, hours = 1;
     
          System.out.print("Enter the vehicle speed: ");
          vehicleSpeed = keyboard.nextInt();
     
          while (vehicleSpeed < 0)
          {
             System.out.print("Enter the vehicle speed: ");
             vehicleSpeed = keyboard.nextInt();
          }
     
          System.out.print("Enter the amount of hours traveled: ");
          hoursTraveled = keyboard.nextInt();
     
          while (hoursTraveled < 1)
          {
             System.out.print("Enter the amount of hours taveled (MPH): ");
             hoursTraveled = keyboard.nextInt();
          }
     
          System.out.println("Hours" + "           Distance Traveled");
          System.out.println("--------------------------------------");
     
          while (hours >= 1)
          {
             System.out.println("  " + hours + "              " + hours * vehicleSpeed + "MPH");
             hours++;
             hoursTraveled--;
          }
       }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Distance Traveled problem help

    while(hours >= 1)
    ...
    hours++

    Look at that bit of code and think about it some more.

  3. The Following User Says Thank You to Harry Blargle For This Useful Post:

    GregBrannon (March 21st, 2014)

Similar Threads

  1. [SOLVED] Distance
    By einar123 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: September 21st, 2013, 06:05 PM
  2. Distance traveled problem
    By Steph2752 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 25th, 2013, 11:43 AM
  3. Distance function help
    By abf617 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 07:32 PM
  4. Getting Distance between two addresses
    By Shockwave786 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 23rd, 2012, 11:19 AM
  5. i am having a problem with the distance formula,help needed!
    By Bentino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2012, 08:07 PM