⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chequedetector.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -