FactKeyInput

FactKeyInput - new InputStreamReader(System.in Loop...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * This program displays factorials as the user enters values interactively */ import java.io.*; // Import all classes in java.io package. Saves typing. // the BufferedReaderClass we will use for keyboard // input is in this package public class FactKeyInput { // this class has two methods (function) // main is the one that the java engine will run automatically // fact will be called in main to compute a factorial / public static void main(String[] args) throws IOException { String line; // to hold user keyboard input long num; // to hold the integer converted from the the user input // This is how we set things up to read lines of text from the user. // after this, we will use the variable "in" to get keyboard typing BufferedReader in = new BufferedReader(
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: new InputStreamReader(System.in) ); // Loop forever (really, until a user signal to quit the loop while (true) { // Display a prompt to the user System.out.print("Compute factorial of what? "); // Read a line from the user. .. line = in.readLine(); // if the user types the word "quit", we will break the loop if ( line.equals("quit") ) { break; } // convert the user's text input to an integer // this is like Number() in JavaScript num = Integer.parseInt(line); // and compute and print the factorial System.out.println("Factorial is "+fact(num)); }// end while }// end method public static long fact (long N) { long fac = 1; int i; for (i=2; i<=N; i++) { fac = fac * i; } return fac; } } // end class...
View Full Document

This note was uploaded on 11/30/2009 for the course COMP 110 taught by Professor Stotts during the Spring '08 term at UNC.

Ask a homework question - tutors are online