/*
Java Programming for Engineers
Julio Sanchez
Maria P. Canton
ISBN: 0849308100
Publisher: CRC Press
*/
// File name: Factorial.java
//Reference: Chapter 10
//
//Java program to demonstrate looping
//Topics:
// 1. Using the for loop
// 2. Loop with multiple processing statements
//
//Requires:
// 1. Keyin class in the current directorypublic class Factorial {  public static void main(String[] args) {    int number;
    int facProd;
    int curFactor;    System.out.println("FACTORIAL CALCULATION PROGRAM");
    number = Keyin.inInt("Enter a positive integer: ");    facProd = number; // Initializing    for (curFactor = number - 1; curFactor > 1; curFactor--) {
      facProd = curFactor * facProd;
      System.out.println("Partial product: " + facProd);
      System.out.println("Current factor:  " + curFactor);
    }    // Display the factorial
    System.out.println("\n\nFactorial is: " + facProd);
  }
}//**********************************************************
//**********************************************************
//Program: Keyin
//Reference: Session 20
//Topics:
//1. Using the read() method of the ImputStream class
//  in the java.io package
//2. Developing a class for performing basic console
//  input of character and numeric types
//**********************************************************
//**********************************************************class Keyin {  //*******************************
  //   support methods
  //*******************************
  //Method to display the user's prompt string
  public static void printPrompt(String prompt) {
    System.out.print(prompt + " ");
    System.out.flush();
  }  //Method to make sure no data is available in the
  //input stream
  public static void inputFlush() {
    int dummy;
    int bAvail;    try {
      while ((System.in.available()) != 0)
        dummy = System.in.read();
    } catch (java.io.IOException e) {
      System.out.println("Input error");
    }
  }  //********************************
  //  data input methods for
  //string, int, char, and double
  //********************************
  public static String inString(String prompt) {
    inputFlush();
    printPrompt(prompt);
    return inString();
  }  public static String inString() {
    int aChar;
    String s = "";
    boolean finished = false;    while (!finished) {
      try {
        aChar = System.in.read();
        if (aChar < 0 || (char) aChar == '\n')
          finished = true;
        else if ((char) aChar != '\r')
          s = s + (char) aChar; // Enter into string
      }      catch (java.io.IOException e) {
        System.out.println("Input error");
        finished = true;
      }
    }
    return s;
  }  public static int inInt(String prompt) {
    while (true) {
      inputFlush();
      printPrompt(prompt);
      try {
        return Integer.valueOf(inString().trim()).intValue();
      }      catch (NumberFormatException e) {
        System.out.println("Invalid input. Not an integer");
      }
    }
  }  public static char inChar(String prompt) {
    int aChar = 0;    inputFlush();
    printPrompt(prompt);    try {
      aChar = System.in.read();
    }    catch (java.io.IOException e) {
      System.out.println("Input error");
    }
    inputFlush();
    return (char) aChar;
  }  public static double inDouble(String prompt) {
    while (true) {
      inputFlush();
      printPrompt(prompt);
      try {
        return Double.valueOf(inString().trim()).doubleValue();
      }      catch (NumberFormatException e) {
        System.out
            .println("Invalid input. Not a floating point number");
      }
    }
  }
}  
 
 

解决方案 »

  1.   

    代码挺乱的,呵呵,应该用编辑栏的#图标,用代码格式贴代码看起来清晰public static int inInt(String prompt) { 
       while(true) {
          inputFlush(); 
          printPrompt(prompt); 
          try { 
             //这里对输入的字符串进行检测,如果不是有效的整数,抛出异常
              //关于Integer.valueOf的具体细节,建议参考JDK帮助文档
             return Integer.valueOf(inString().trim()).intValue(); 
          } catch(NumberFormatException e) {
             System.out.println("Invalid input. Not an integer"); 
          }
       }
    }