A Working (but useless) Program
With the blanks filled in the program
can be compiled and run
(perhaps you will import it into NotePad and try it?).
It doesn't do much,
but the basic user interaction is complete.
import java.io.*;
class evalPoly
{
public static void main (String[] args ) throws IOException
{
BufferedReader userin = new BufferedReader(new InputStreamReader(System.in) );
double x; // a value to use with the polynomial
String response = "y"; // "y" or "n"
while ( response.equals( "y" ) )
{
// get a value for x
// evaluate the polynomial
// print out the result
// ask the user if program should continue
// the user's answer will be referenced by "response"
System.out.println("continue (y or n)?");
response = userin.readLine();
}
}
}
|
Here is an example of this exciting program running:
Symantec Java! JustInTime Compiler Version 210.063 for JDK 1.1.3
Copyright (C) 1996-97 Symantec Corporation
continue (y or n)?
y
continue (y or n)?
y
continue (y or n)?
n
As with all loops, there were three things to get right:
- Initializing the loop.
- Testing if the loop should continue.
- Setting up for the next test.
In order for this loop to work correctly, the initial value of
response was initialized to "y".
|