Results 1 to 6 of 6
  1. #1
    Zeronitic is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default 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");
            }
        }
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default 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?

  3. #3
    Zeronitic is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need help ending a loop

    Quote Originally Posted by Norm View Post
    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?

    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

    bye
    Last edited by Zeronitic; 01-26-2012 at 11:30 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default 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.

  5. #5
    Zeronitic is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need help ending a loop

    Nevermind. I solved this myself. Thanks for the help.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

Similar Threads

  1. Ending Program With While Loop
    By method22 in forum New To Java
    Replies: 3
    Last Post: 11-28-2011, 06:40 PM
  2. ending a csv
    By paul1024 in forum New To Java
    Replies: 2
    Last Post: 05-04-2011, 02:14 AM
  3. Problem with Ending Craps game
    By Ebayer in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 07:17 PM
  4. ending a graphics class
    By imorio in forum AWT / Swing
    Replies: 11
    Last Post: 08-16-2010, 05:41 PM
  5. [SOLVED] Ending for loop with a String
    By jdiesel in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 02:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •