Results 1 to 4 of 4

Thread: Sentinel Loop

  1. #1
    pricklypete is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Sentinel Loop

    Evening everyone. Had a quick question about a sentinel loop. Ideally I want the program to terminate as soon as the user enters "-9999".

    I've got it to the point where the loop will end EVENTUALLY after user enters "-9999", but the "Enter the Wind Velocity" Prompt still comes up after the user enters the sentinel -9999. I want the program to end immediately upon receiving the sentinel - I don't want any more prompts after receiving the sentinel.

    I don't know, but Good Lord I've spent the entire day trying to figure out just this last piece of the program. I'm at a loss and would really appreciate any advice I can get. Below is what I have so far. Please, I'll take anything!!!!





    import java.util.*;
    public class windChill999 {
    public static void main(String[] args) {




    Scanner console = new Scanner(System.in);

    double x;
    do{
    System.out.printf("Enter the temperature Degrees Fahrenheit (Enter -9999 to stop): ");
    double tempF = console.nextDouble();
    x = tempF;







    System.out.printf("Enter the Wind Velocity (MPH): ");

    double velocityMPH = console.nextDouble();
    double chillTemp = 35.74 + 0.6215*tempF - 35.75*Math.pow(velocityMPH, 0.16) +
    0.4275*tempF*Math.pow(velocityMPH, 0.16);

    System.out.printf("The wind chill for temp (degrees F) = %6.1f", tempF);
    System.out.printf(" and wind velocity (MPH) = %6.1f", velocityMPH);
    System.out.printf(" is = %6.1f degress fahrenheit\n", chillTemp);

    }while(x != -9999);
    }

    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Sentinel Loop

    You could use a "while(true) {...}" loop and have a break statement at the appropriate place.

    In any case declare x inside the loop because it is only used inside the loop. And follow Java naming conventions and call the class WindChill999 with a capital 'W'.

  3. #3
    pricklypete is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Sentinel Loop

    Nah, I figured it out...here it is:


    import java.util.*;
    public class windChill999 {
    public static void main(String[] args) {




    Scanner console = new Scanner(System.in);


    System.out.printf("Enter the temperature Degrees Fahrenheit (Enter -9999 to stop): ");
    double x;
    do{

    double tempF = console.nextDouble();
    x = tempF;







    System.out.printf("Enter the Wind Velocity (MPH): ");

    double velocityMPH = console.nextDouble();
    double chillTemp = 35.74 + 0.6215*tempF - 35.75*Math.pow(velocityMPH, 0.16) +
    0.4275*tempF*Math.pow(velocityMPH, 0.16);

    System.out.printf("The wind chill for temp (degrees F) = %6.1f", tempF);
    System.out.printf(" and wind velocity (MPH) = %6.1f", velocityMPH);
    System.out.printf(" is = %6.1f degress fahrenheit\n", chillTemp);

    System.out.printf("Enter the temperature Degrees Fahrenheit (Enter -9999 to stop): ");
    tempF = console.nextDouble();

    }while(x != -9999);
    }

    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Sentinel Loop

    I want the program to end immediately upon receiving the sentinel
    If the user enters -9999 the first time they are prompted the program will not end immediately.

Similar Threads

  1. Sentinel value and variable scope
    By jonathan in forum New To Java
    Replies: 2
    Last Post: 07-21-2012, 08:19 AM
  2. Sentinel value in a while loop
    By Mike Miller in forum New To Java
    Replies: 9
    Last Post: 09-30-2011, 02:04 AM
  3. Counting with a sentinel loop
    By Teclis in forum New To Java
    Replies: 0
    Last Post: 03-22-2011, 07:38 PM
  4. sentinel help
    By droidus in forum New To Java
    Replies: 12
    Last Post: 03-04-2011, 02:58 PM
  5. Help with Sentinel Loops
    By hedwards09 in forum New To Java
    Replies: 11
    Last Post: 11-07-2009, 07:39 PM

Posting Permissions

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