Results 1 to 6 of 6
Thread: Need help ending a loop
- 01-26-2012, 10:42 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Need help ending a loop
I need to end this in Initial miles by entering -1.
Someone told said to use a break statement but I don't know how to use that.
Any ideas on how to end the loop with -1?
Thanks if anyone can help.
Java Code:import java.util.*; public class break{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int gallons; int MPG; int a; int initial = 0; do { System.out.print("Initial miles: "); initial = scan.nextInt(); System.out.print("Final miles: "); int final1 = scan.nextInt(); a = final1-initial; System.out.print("Gallons: "); gallons = scan.nextInt(); MPG = a/gallons; System.out.println("Miles per gallon: " + MPG); } while ( initial >= 0); { if (initial < 0) { System.out.println("bye"); } } } }
- 01-26-2012, 11:15 PM #2
Re: Need help ending a loop
Where in the loop do you want to exit?
You should find in your textbook or tutorial and read about the break and continue statements. Both are useful in controlling the execution flow in a loop.
break exits,
continues skips to the top.
Your usage of {} are confusing. The {} following the while for example. What are they being used for?
- 01-26-2012, 11:27 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Need help ending a loop
The {} are supposed to show "bye" if initial is less than 0.
I'm not too fond of this since I'm in homeschool and I don't have my teacher to give me tips at the moment.
I need it to end it to end when I enter -1 in Initial miles.
-1 is a sentinel value to end the loop but isn't working when I use it.
This is how the program is supposed to start and to end.
Initial miles:
15000
Final miles:
15250
Gallons
10
Miles per Gallon: 25.0
Initial miles:
107000
Final miles:
107450
Gallons
15
Miles per Gallon: 30.0
Initial miles:
-1
byeLast edited by Zeronitic; 01-26-2012 at 11:30 PM.
- 01-26-2012, 11:43 PM #4
Re: Need help ending a loop
Where in the loop, after what statement do you want to exit the loop?
At that point in the loop, add code(an if statement) to test if the user has entered the end the loop value.
If the test is true, use the break statement.
You can remove the {}s that follow the while statement: line 30 & 35. They serve no purpose.
- 01-27-2012, 12:24 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Need help ending a loop
Nevermind. I solved this myself. Thanks for the help.
- 01-27-2012, 01:25 AM #6
Similar Threads
-
Ending Program With While Loop
By method22 in forum New To JavaReplies: 3Last Post: 11-28-2011, 06:40 PM -
ending a csv
By paul1024 in forum New To JavaReplies: 2Last Post: 05-04-2011, 02:14 AM -
Problem with Ending Craps game
By Ebayer in forum New To JavaReplies: 4Last Post: 02-11-2011, 07:17 PM -
ending a graphics class
By imorio in forum AWT / SwingReplies: 11Last Post: 08-16-2010, 05:41 PM -
[SOLVED] Ending for loop with a String
By jdiesel in forum New To JavaReplies: 3Last Post: 01-08-2009, 02:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks