FileDocCategorySizeDatePackage
FactQuoter.javaAPI DocExample1412Mon Sep 22 13:30:30 BST 1997None

FactQuoter

public class FactQuoter extends Object
This program displays factorials as the user enters values interactively

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    // This is how we set things up to read lines of text from the user.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    // Loop forever
    for(;;) {
      // Display a prompt to the user
      System.out.print("FactQuoter> ");
      // Read a line from the user
      String line = in.readLine();
      // If we reach the end-of-file, or if the user types "quit", then quit
      if ((line == null) || line.equals("quit")) break;
      // Try to parse the user's input, and compute and print the factorial
      try { 
        int x = Integer.parseInt(line);
        System.out.println(x + "! = " + Factorial4.factorial(x)); 
      }
      // If anything goes wrong, display a generic error message
      catch(Exception e) { System.out.println("Invalid Input"); }
    }