久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

Java Socket實現(xiàn)猜數(shù)字小游戲

瀏覽:40日期:2022-08-24 08:48:49

本文實例為大家分享了Java Socket實現(xiàn)猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下

運行截圖

Server

Java Socket實現(xiàn)猜數(shù)字小游戲

Client

Java Socket實現(xiàn)猜數(shù)字小游戲

完整代碼

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)建一個服務(wù)器 System.out.println('服務(wù)端啟動,等待客戶端連接。。。'); 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() + ' 已成功連接到此臺服務(wù)器上。'); //字符輸出流 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('正在向服務(wù)器請求連接。。。'); 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ù)字:'); //先讀取鍵盤錄入方可向服務(wù)端發(fā)送消息 getKey = new Scanner(System.in); while (getKey.hasNextLine()) { //寫到服務(wù)端的的控制臺 out.println(getKey.nextLine()); out.flush(); try { System.out.println('提示消息:' + getServer.nextLine()); System.out.print('請輸入數(shù)字:'); } catch (Exception e) { System.out.print('游戲結(jié)束!!!'); break; } //阻塞等待接收服務(wù)端的消息 } } 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)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

java經(jīng)典小游戲匯總

javascript經(jīng)典小游戲匯總

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产高潮在线观看 | www.伊人.com| 国产三区在线视频 | 天堂√在线观看一区二区 | 国产在线中文字幕 | 国产99久久久久久免费看农村 | 四虎影院最新网站 | 免费亚洲网站 | 国产精品久久久久久中文字 | 欧美日韩精品久久久 | 九九r热 | 午夜精品久久久久久久久 | 91精品一区二区 | 亚洲综合大片69999 | av一区二区三区四区 | 日韩精品极品视频在线 | 香港三级日本三级a视频 | 色婷综合网 | 亚洲高清资源 | 中文字幕第90页 | 久久精品久久久久电影 | 久久精品视频在线播放 | 国产精品a免费一区久久电影 | 国产精品久久免费观看spa | 国产wwwcom| 久草.com| 蜜桃视频在线观看www社区 | 亚洲成人av一区二区三区 | 亚洲第一视频 | 青青草国产| 午夜小电影 | 日本一区二区高清不卡 | 久久99精品国产麻豆婷婷洗澡 | 九九只有精品 | 国产97色在线 | 亚洲 | 亚洲色图3p | 日韩欧美不卡 | av一区二区三区四区 | 国产第一区在线观看 | 红杏aⅴ成人免费视频 | 99国内精品久久久久久久 |