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: Setting variable correctly in sentinel-controlled loop

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Setting variable correctly in sentinel-controlled loop

    Hi All, I'm only up to chapter 4 in an intro to java book (Deitel), so I'm sure this isn't the most efficient code, but there's just one thing I'm trying to learn: to get the code to execute correctly, I had to set my sentinel variable tripMiles twice, once inside the while statement and once before it.

    Once before the while statement and once inside it. This seems redundant but is the only way I got it to work by trial and error. Anyone who'd like to educate me, I'd appreciate it.

    import java.util.Scanner;

    public class MPGs //begin class, MPGs
    {
    public static void main(String[] args)//main method for starting application
    {
    //declare variables
    int tripMiles = 0;
    int tripGas = 0;
    int milesTotal = 0;
    int gasTotal = 0;
    double tripMpg = 0;
    double mpgTotal = 0;

    //initialize variables (necessary?)

    Scanner input = new Scanner ( System.in);
    System.out.println("Enter miles traveled this trip (or -1 to end program):");
    tripMiles = input.nextInt();

    // sentinel control
    while (tripMiles >= 0)
    {

    System.out.println("Enter gas used this trip:");
    tripGas = input.nextInt();

    milesTotal = tripMiles + milesTotal;
    gasTotal = tripGas + gasTotal;
    tripMpg = (double) tripMiles / (double) tripGas;
    mpgTotal = (double) milesTotal / (double) gasTotal;
    System.out.printf("Miles total is: %d. MPG for this trip is %.2f.\n", milesTotal, tripMpg);
    System.out.printf("Average MPG across all trips is %.3f.\n\n", mpgTotal);
    //a second request for Miles for this trip

    System.out.print ("Enter miles traveled this trip (or -1 to end program):");
    tripMiles = input.nextInt();

    }
    System.out.println("Goodbye!");
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setting variable correctly in sentinel-controlled loop

    Its usually a good idea to give a value to a variable that is local to a method when you define it.
    You could wait and define it at the same time as you read a value into it:
    int tripMiles = input.nextInt();
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. input controlled loop
    By pezbirdy in forum Loops & Control Statements
    Replies: 4
    Last Post: November 10th, 2011, 06:16 AM
  2. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 09:13 PM
  3. Help with counter controlled while loop please!
    By rockout341 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 15th, 2011, 02:41 PM
  4. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM
  5. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM