3
回答
格式化输入类,代码中有一些不懂,请高人指点一二
开发十年,就只剩下这套Java开发体系了   
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
 
class InvalidUserInputException extends Exception {
      public InvalidUserInputException() {
        super("Invalid user exception");
      }
 
      public InvalidUserInputException(String message) {
        super(message);
      }
      private static final long serialVersionUID = 90001L; //这里把serialVersionUID设置为90001L有什么用?
    }
 
class FormattedInput {
 
  public int readInt() throws InvalidUserInputException {
    if (readToken() != StreamTokenizer.TT_NUMBER) { //这里readToken代表什么,StreamTokenizer.TT_NUMBER代表什么
      throw new InvalidUserInputException("readInt() failed. Input data not numeric.");
    }
 
    if (tokenizer.nval > (double) Integer.MAX_VALUE //tokenizer.nval代表什么? nval代表什么
            || tokenizer.nval < (double) Integer.MIN_VALUE) {
      throw new InvalidUserInputException("readInt() failed. Input outside range of type int.");
    }
 
    if (tokenizer.nval != (double) (int) tokenizer.nval) {
      throw new InvalidUserInputException("readInt() failed. Input not an integer");
    }
    return (int) tokenizer.nval;
  }
 
  public double readDouble() throws InvalidUserInputException {
    if (readToken() != StreamTokenizer.TT_NUMBER) {
      throw new InvalidUserInputException("readDouble() failed. Input data not numeric.");
    }
    return tokenizer.nval;
  }
 
  public String readString() throws InvalidUserInputException {
    if (readToken() == StreamTokenizer.TT_WORD || ttype == '\"'
            || ttype == '\"') {
      return tokenizer.sval;
    } else {
      throw new InvalidUserInputException("readString() failed. Input data is not a string.");
    }
  }
   
  private int readToken() { //这个成员函数好像一直都没有被执行到,它 在这里有什么用 ?
    try {
      ttype = tokenizer.nextToken(); //tokenizer.nextToken()有什么用? 
      return ttype;
 
    } catch (IOException e) {                                          
      e.printStackTrace();
      System.exit(1);                                             
    }
    return 0;
  }
 
  
  private StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); //这一句好像也不会被执行?
  private int ttype;                                          
}
 
public class TestFormattedInput {
      public static void main(String[] args) {
        FormattedInput kb = new FormattedInput();
        for (int i = 0; i < 5; ++i) {
          try {
            System.out.print("Enter an integer:");
            System.out.println("Integer read:" + kb.readInt());
            System.out.print("Enter a double value:");
            System.out.println("Double value read:" + kb.readDouble());
            System.out.print("Enter a string:");
            System.out.println("String read:" + kb.readString());
          } catch (InvalidUserInputException e) {
            System.out.println("InvalidUserInputException thrown.\n" + e.getMessage());
          }
        }
      }
    }
//不太懂的地方,希望高人能够说详细点,谢谢
<无标签>
举报
jrx
发帖于5年前 3回/171阅
顶部