Results 1 to 4 of 4
Thread: Sentinel Loop
- 11-10-2012, 02:32 AM #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);
}
}
- 11-10-2012, 03:19 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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'.
- 11-10-2012, 03:32 AM #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);
}
}
- 11-10-2012, 04:57 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
Sentinel value and variable scope
By jonathan in forum New To JavaReplies: 2Last Post: 07-21-2012, 08:19 AM -
Sentinel value in a while loop
By Mike Miller in forum New To JavaReplies: 9Last Post: 09-30-2011, 02:04 AM -
Counting with a sentinel loop
By Teclis in forum New To JavaReplies: 0Last Post: 03-22-2011, 07:38 PM -
sentinel help
By droidus in forum New To JavaReplies: 12Last Post: 03-04-2011, 02:58 PM -
Help with Sentinel Loops
By hedwards09 in forum New To JavaReplies: 11Last Post: 11-07-2009, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks