Results 1 to 6 of 6
- 02-21-2012, 07:28 PM #1
Member
- Join Date
- Feb 2012
- Location
- Philadelphia
- Posts
- 10
- Rep Power
- 0
Calculating Mileage & Breaking For Loop
Hey all,
I'm trying to create an application that calculates total mileage traveled and subtracts the initial mileage reading on the odometer. Can someone please tell me what I'm doing wrong? Also, can you tell me how I can break the For loop by inputting a negative number or typing in "stop"? I pasted the code below. Thanks for your help!
import java.util.Scanner;
public class Lab4{
public static void main(String args[]){
int miles[] = new int[10];
double gallons[] = new double [10];
double mpg[] = new double [10];
Scanner kb = new Scanner(System.in);
int count=0;
double counter=0.0;
for (int i = 0; i < 1; i++){
System.out.println("What is the reading on the odometer?");
miles = kb.nextInt();
if (i > 0)
count = count + miles;
}
for (int i = 1; i < 9; i++){
System.out.println("How many miles have you traveled since last filling up?");
miles = kb.nextInt();
if (i > 0);
count = count + miles - miles[0];
}
System.out.print("You have traveled " + count);
System.out.println(" miles");
- 02-21-2012, 07:51 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Calculating Mileage & Breaking For Loop
You tell us...does this compile? Are there exceptions? Does it misbehave? Provide context to your problem and your chances of receiving help in a reasonable amount of time skyrocket.Can someone please tell me what I'm doing wrong?
- 02-21-2012, 08:17 PM #3
Member
- Join Date
- Feb 2012
- Location
- Philadelphia
- Posts
- 10
- Rep Power
- 0
Re: Calculating Mileage & Breaking For Loop
Sorry about that. When I run the program, I don't get any errors or exceptions. I tried formatting it a different way, and I think I have it working much better now(I pasted the new code below). What can I do so that the user can cut out of the loop early, instead of always having to type in a number for the mileage 10 times? Can I create something that says "if the user inputs a negative number or a certain word, then the loop will stop"?
import java.util.Scanner;
public class Lab4{
public static void main(String args[]){
int miles[] = new int[10];
//double gallons[] = new double [10];
//double mpg[] = new double [10];
Scanner kb = new Scanner(System.in);
int count=0;
for (int i = 0; i < 10; i++){
System.out.println("How many miles have you traveled since last filling up?");
miles[i] = kb.nextInt();
if (i > 0);
count = count + miles [i];
}
System.out.print("You have traveled " + count);
System.out.println(" miles");
//for (double j = 0; j < 10; j++){
//System.out.println("How many gallons did you fill up?");
//gallons[j] = kb.nextDouble();
//if (j > 0.0)
//counter = counter + gallons;
}
}
- 02-21-2012, 09:15 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Calculating Mileage & Breaking For Loop
Why not? Just evaluate what the user entered and if its negative then break out of the loop (and indicate to the user this through the println statement).Can I create something that says "if the user inputs a negative number or a certain word, then the loop will stop"?
- 02-22-2012, 12:46 AM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Calculating Mileage & Breaking For Loop
Output:Java Code:import java.util.Scanner; public class Lab4{ public static void main(String args[]){ int miles[] = new int[10]; //double gallons[] = new double [10]; //double mpg[] = new double [10]; Scanner kb = new Scanner(System.in); int count=0; for (int i = 0; i < 10; i++){ System.out.println("How many miles have you traveled since last filling up?"); int addMiles = kb.nextInt(); if(addMiles > 0) { miles[i] = addMiles; count = count + miles [i]; } else { i = 9; } } System.out.print("You have traveled " + count); System.out.println(" miles"); //for (double j = 0; j < 10; j++){ //System.out.println("How many gallons did you fill up?"); //gallons[j] = kb.nextDouble(); //if (j > 0.0) //counter = counter + gallons; } }
How many miles have you traveled since last filling up?
43
How many miles have you traveled since last filling up?
4
How many miles have you traveled since last filling up?
-1
You have traveled 47 miles
- 02-22-2012, 12:51 AM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Calculating Mileage & Breaking For Loop
@fatabass, recommended reading: The Problems With Spoonfeeding
Similar Threads
-
Breaking out a timer loop
By musico in forum New To JavaReplies: 6Last Post: 06-23-2011, 08:29 PM -
Breaking the thread.
By JDaniel in forum Threads and SynchronizationReplies: 4Last Post: 12-21-2010, 03:22 PM -
breaking up
By smray7 in forum New To JavaReplies: 2Last Post: 10-31-2010, 07:58 AM -
breaking out of while loop
By mac in forum New To JavaReplies: 5Last Post: 05-18-2010, 03:21 PM -
Breaking up of array
By agarwal_srushti in forum New To JavaReplies: 3Last Post: 09-27-2009, 07:03 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks