7 Replies - 1560 Views - Last Post: 15 October 2012 - 12:47 AM Rate Topic: -----

#1 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 140
  • Joined: 12-October 12

simple looping problem , problem with displaying

Posted 14 October 2012 - 11:38 PM

been working on this problem that asks:

1) Ask the user for a speed in miles per hours.
2) Ask the user for the number of hours that the car has traveled.
3) The input should be validated so if the user inputs a negative value for either request. you should reprompt the user to give a valid answer.
4)Using the given information from the user display a table that shows the distance traveled for each hour.

Sample Run:
Enter the speed of the car in miles per hour: 40
Enter the total hours of travel time: 3

Hour Distance Traveled
1 40
2 80
3 120

now ive got most of it done ( pretty much all done) but i dont get how to display the distance traveled for EACH hour. right now , my program does everything , except it displays just the hour entered and the total distance. any tips for this?

import java.util.Scanner;


     public class Distance{
         public static void main(String[] args) {
		
		 // creates the scanner
          Scanner input = new Scanner(System.in);
		  // promt user for input
		
		  
		  System.out.println("Enter the speed of the car in miles per hour: ");
		  int speed = input.nextInt();
		  System.out.println("Enter the total hours of travel time: ");
		  int hours = input.nextInt();
		  //Display the table headings
		
		while ( speed <0 && hours <0){
		 
			  System.out.println("Please enter a Valid Answer! ");
			  
			  System.out.println("Enter the speed of the car in miles per hour: ");
		      speed = input.nextInt();
		 
		      System.out.println("Enter the total hours of travel time: ");
		      hours = input.nextInt();

			  }
			  System.out.println("Hours\tDistance Travaled(miles)");
		      System.out.println("---------------------------------");
		
	          System.out.println(hours+ "\t" + (speed * hours));
		
		  
		  }
		  
	 }


]

Is This A Good Question/Topic? 0
  • +

Replies To: simple looping problem , problem with displaying

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: simple looping problem , problem with displaying

Posted 14 October 2012 - 11:47 PM

Shouldn't it just be like this:
distance = total travelled hours - travelled hours from last hour
Was This Post Helpful? 0
  • +
  • -

#3 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 140
  • Joined: 12-October 12

Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:07 AM

aaahhh!! so close! now i got it printing each hour , but the distance stays the same. now i gotta figure that out

import java.util.Scanner;


     public class Distance{
         public static void main(String[] args) {
		
		 // creates the scanner
          Scanner input = new Scanner(System.in);
		  // promt user for input
		
		  
		  System.out.println("Enter the speed of the car in miles per hour: ");
		  double speed = input.nextDouble();
		  System.out.println("Enter the total hours of travel time: ");
		  double hours = input.nextDouble();
		  int i;
		
		
		while ( speed <0 && hours <0){
		 
			  System.out.println("Please enter a Valid Answer! ");
			  
			  System.out.println("Enter the speed of the car in miles per hour: ");
		      speed = input.nextInt();
		 
		      System.out.println("Enter the total hours of travel time: ");
		      hours = input.nextInt();

			  }
			 
			    //Display the table headings
			  System.out.println("Hours\tDistance Travaled(miles)");
		      System.out.println("---------------------------------");
		for (i = 1; i <= hours; i++){
                  

	          System.out.println(i+ "\t" + (speed * hours));
		     }
		  
		  }
		  
	 }



Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:20 AM

It seems to me your program is designed so that for each hour there is a static distance. Then wouldn't it be easier to do distance divided by hour?
Was This Post Helpful? 0
  • +
  • -

#5 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 140
  • Joined: 12-October 12

Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:31 AM

View PostfromTheSprawl, on 15 October 2012 - 12:20 AM, said:

It seems to me your program is designed so that for each hour there is a static distance. Then wouldn't it be easier to do distance divided by hour?


i tried that , but it displays it backwards. it prints

1. 120
2. 60
3. 40

i had
System.out.println(i+ "\t" +((speed*hours)/i));


Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:40 AM

Wouldn't it be just iterating through the total hours and printing the input distance since that's the result anyway?

Oh, I see. Just multiply the current value of i to the initial speed and you'll get what you want.
Was This Post Helpful? 0
  • +
  • -

#7 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 140
  • Joined: 12-October 12

Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:42 AM

woo , i got it working lol. thanks for the suggestions. just to substitute i into my distance formula. :bananaman:

View PostfromTheSprawl, on 15 October 2012 - 12:40 AM, said:

Wouldn't it be just iterating through the total hours and printing the input distance since that's the result anyway?

Oh, I see. Just multiply the current value of i to the initial speed and you'll get what you want.

haha yeah , you replyed just as i found that out. thanks for reply anyhow! thats exactly what i had to do
Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:47 AM

Awesome! Congratulations. ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1