chequedetector.java

来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 66 行

JAVA
66
字号
import java.io.*;
import java.io.*;
import javagently.*;

public class ChequeDetector {

  /* Counterfeit cheque detector    by J M Bishop Sept 1997
   * ---------------------------    Java 1.1 October 1997
   *                                updated July 2000
   * Checks a cheque number for the occurrence
   * of >= 3 zeros
   * or >= 4 non-zeros in a row
   * The digits of the number must be separated by spaces.
   *
   * Illustrates if-then-else and characters
   */

  static final int noOfDigits = 10;

  boolean counterfeit = false;
  int countOfZeros = 0;
  int countOfNonzeros = 0;

  ChequeDetector () throws IOException {

    Stream in = new Stream (System.in);

    System.out.println("***** Checking for counterfeits *****");
    System.out.println("Enter a cheque number of ten digits "+
        "separated by a space and ending with a return");

    char digit;

    for (int i = 0; i < noOfDigits; i++) {
      digit = in.readChar();
      if (digit == '0')
        recordZero();
      else
        recordNonzero();
    }
    if (counterfeit)
      System.out.print("\tCOUNTERFEIT");
    else
      System.out.print("\tOK");
    System.out.println();
  }

  void recordZero() {
    countOfZeros ++;
    countOfNonzeros = 0;
    if (countOfZeros == 3)
      counterfeit = true;
  }

  void recordNonzero() {
    countOfNonzeros ++;
    countOfZeros = 0;
    if (countOfNonzeros == 4)
      counterfeit = true;
  }
  public static void main(String[] args) throws IOException {
    new ChequeDetector();
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?