Java Socket實現(xiàn)猜數(shù)字小游戲
更新時間:2020年09月24日 11:48:25 作者:清瀟和梨花
這篇文章主要為大家詳細介紹了Java Socket實現(xiàn)猜數(shù)字小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java Socket實現(xiàn)猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下
運行截圖
Server

Client

完整代碼
Server
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.Scanner;
public class ServerDemo {
private int flag;
public void server() throws IOException {
//創(chuàng)建一個服務器
System.out.println("服務端啟動,等待客戶端連接。。。");
PrintWriter out = null;
Scanner getClient = null;
ServerSocket server = null;
Socket client = null;
this.setFlag();
try {
server = new ServerSocket(6666);
//創(chuàng)建一個接收連接客戶端的對象
client = server.accept();
System.out.println(client.getInetAddress() + " 已成功連接到此臺服務器上。");
//字符輸出流
out = new PrintWriter(client.getOutputStream()); //向客戶端發(fā)送數(shù)據(jù)
out.println("歡迎來到猜數(shù)字小游戲(1-100)!?。?);
out.flush();//將緩沖流中的數(shù)據(jù)全部輸出
getClient = new Scanner(client.getInputStream()); //接收客戶端發(fā)送的數(shù)據(jù)
//阻塞等待客戶端發(fā)送消息過來
while (getClient.hasNextLine()) {
String returnMsg = null;
String tmp = getClient.nextLine();
if ("e".equals(tmp)) {
break;
} else if ("c".equals(tmp)) {
returnMsg = "歡迎來到新一輪的猜數(shù)字小游戲(1-100)?。?!";
this.setFlag();
} else {
int clientInput = Integer.parseInt(tmp);
if (clientInput > flag) {
returnMsg = "你輸入的數(shù)字大了!?。?;
} else if (clientInput < flag) {
returnMsg = "你輸入的數(shù)字小了!??!";
} else {
returnMsg = "恭喜你,猜中了?。?!繼續(xù)(輸入c),退出(輸入e)";
}
}
out.println(returnMsg); //向客戶端發(fā)送數(shù)據(jù)
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
out.close();
getClient.close();
server.close();
}
}
// 生成隨機數(shù)字
public void setFlag() {
Random r = new Random();
flag = r.nextInt(100);
System.out.println("猜數(shù)字小游戲答案:flag = " + flag);
}
public static void main(String[] args) throws IOException {
new ServerDemo().server();
}
}
Client
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class ClientDemo {
public void client() throws IOException {
System.out.println("正在向服務器請求連接。。。");
Socket client = null;
Scanner getKey = null;
Scanner getServer = null;
PrintWriter out = null;
try {
client = new Socket("127.0.0.1", 6666);
getServer = new Scanner(client.getInputStream());
System.out.println(getServer.nextLine());
out = new PrintWriter(client.getOutputStream());
System.out.print("請輸入數(shù)字:");
//先讀取鍵盤錄入方可向服務端發(fā)送消息
getKey = new Scanner(System.in);
while (getKey.hasNextLine()) {
//寫到服務端的的控制臺
out.println(getKey.nextLine());
out.flush();
try {
System.out.println("提示消息:" + getServer.nextLine());
System.out.print("請輸入數(shù)字:");
} catch (Exception e) {
System.out.print("游戲結束!??!");
break;
}
//阻塞等待接收服務端的消息
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
getKey.close();
getServer.close();
out.close();
client.close();
}
}
public static void main(String[] args) throws IOException {
new ClientDemo().client();
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
兩個小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作
這篇文章主要介紹了兩個小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringBoot熔斷機制之CircuitBreaker詳解
這篇文章主要介紹了SpringBoot熔斷機制之CircuitBreaker詳解,SpringBoot的熔斷機制在微服務架構中扮演著重要角色,其中CircuitBreaker是其核心機制之一,用于防止服務的異常狀態(tài)影響到整個系統(tǒng)的運作,需要的朋友可以參考下2023-10-10
淺析SpringBoot中使用thymeleaf找不到.HTML文件的原因
這篇文章主要介紹了SpringBoot中使用thymeleaf找不到.HTML文件的原因分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Spring依賴注入中的@Resource與@Autowired詳解
這篇文章主要介紹了Spring依賴注入中的@Resource與@Autowired詳解,提到Spring依賴注入,大家最先想到應該是@Resource和@Autowired,對于Spring為什么要支持兩個這么類似的注解卻未提到,屬于知其然而不知其所以然,本文就來做詳細講解,需要的朋友可以參考下2023-09-09

