Why is this loop a sentinel-controlled loop?
The sentinel value "n" will terminate the loop.
Here is the program, with some work done on reading a value for "x".
import java.io.*; class evalPoly { public static void main (String[] args ) throws IOException { BufferedReader userin = new BufferedReader(new InputStreamReader(System.in) ); String xChars; // character version of x, from the user double x; // a value to use with the polynomial String response = "y"; // "y" or "n" while ( response.equals( "y" ) ) { // prompt the user and get a value for x __________________________________________ ; __________________________________________ ; x = ( Double.valueOf( xChars ) ).doubleValue(); // evaluate the polynomial // print out the result // ask the user if program should continue System.out.println("continue (y or n)?"); response = userin.readLine(); } } } |
If you need a review of how to convert character data to floating point form, look at chapter 8. This part has been done for you already (the statement in red).