1. Member
Join Date
Nov 2012
Posts
3
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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. Member
Join Date
Nov 2012
Posts
3
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

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

#### Posting Permissions

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