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

📄 clientnumber.java

📁 使用Java 编制的小游戏
💻 JAVA
字号:
import java.io.*;
import java.net.*;

public class ClientNumber {

	private Socket soc;

	DataInputStream din;

	DataOutputStream dout;

	BufferedReader bin;

	public static final int PORT = 8181;

	String s;

	ClientNumber() {

		startClient();
		inputServerMessage();
	}

	void startClient() {
		try {
			soc = new Socket(InetAddress.getByName(null), PORT);// 初始化客户端套接字;
			din = new DataInputStream(soc.getInputStream());
			dout = new DataOutputStream(soc.getOutputStream());

		} catch (UnknownHostException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	void inputServerMessage() {

		try {
			String sbuf;

			bin = new BufferedReader(new InputStreamReader(System.in));

			while (true) {
				if ((s = din.readUTF()).equals("恭喜您答对了")) {
					System.out.println(s + "游戏退出");
					break; // 判断正确退出循环;
				}
				System.out.println(s);
				if (s.subSequence(0, 1).equals("w")) {// 当判断次数过多被警告后;
					if (exitOrRestart())// 如果命令为exit就要退出循环;
					{
						break;

					} else {
						restart();
						continue;
					}

				}

				sbuf = bin.readLine();

				if (sbuf.equals("exit"))// 直接键盘输入exit退出循环;
					break;
				dout.writeUTF(sbuf);

			}
		} catch (IOException e) {

			e.printStackTrace();
		}

		exit();
	}

	boolean exitOrRestart() {

		try {
			String s = "";

			do {
				s = bin.readLine();
				if (s.equals("exit")) {

					return true;
				}
				if (s.equals("restart")) {
					 
					
					return false;
				}

				System.out.println("重新输入,命令不对");
			} while (true);

		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return false;
	}

	void exit() {

		try {
			dout.writeUTF("exit");
			din.close();
			bin.close();
			dout.close();

			soc.close();
		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	void restart() {
		try {
			dout.writeUTF("restart");
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new ClientNumber();

	}

}

⌨️ 快捷键说明

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