serverprotocoltest.java

来自「JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程」· Java 代码 · 共 69 行

JAVA
69
字号
package ch07.section07;

public class ServerProtocolTest {
  private static final int WAITING = 0;
  private static final int SENTKNOCKKNOCK = 1;
  private static final int SENTCLUE = 2;
  private static final int ANOTHER = 3;

  private static final int NUMJOKES = 5;

  private int state = WAITING;
  private int currentJoke = 0;

  private String[] clues = {
      "1", "2", "3", "4", "5"};
  private String[] answers = {
      "6",
      "7",
      "8",
      "9",
      "10"};

  public String processInput(String theInput) {
    String theOutput = null;

    if (state == WAITING) {
      theOutput = "Holle";
      state = SENTKNOCKKNOCK;
    }
    else if (state == SENTKNOCKKNOCK) {
      if (theInput.equalsIgnoreCase("你好!")) {
        theOutput = clues[currentJoke];
        state = SENTCLUE;
      }
      else {
        theOutput = "8888888";
      }
    }
    else if (state == SENTCLUE) {
      if (theInput.equalsIgnoreCase(clues[currentJoke] + " string98")) {
        theOutput = answers[currentJoke] + " yyyyy";
        state = ANOTHER;
      }
      else {
        theOutput = "rrrrrr" +
            "hhhhhhhhhh";
        state = SENTKNOCKKNOCK;
      }
    }
    else if (state == ANOTHER) {
      if (theInput.equalsIgnoreCase("y")) {
        theOutput = "565656";
        if (currentJoke == (NUMJOKES - 1)) {
          currentJoke = 0;
        }
        else {
          currentJoke++;
        }
        state = SENTKNOCKKNOCK;
      }
      else {
        theOutput = "Bye.";
        state = WAITING;
      }
    }
    return theOutput;
  }
}

⌨️ 快捷键说明

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