JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能
本文實(shí)例為大家分享了JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能的具體代碼,供大家參考,具體內(nèi)容如下
先看下文件結(jié)構(gòu)
使用到的知識(shí)點(diǎn):
看下Client類的實(shí)現(xiàn):
package SocketS; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; import org.apache.log4j.Logger; import com.sun.security.ntlm.Client; import User.Users; import User.UsersDao; /** * 客戶端調(diào)用登錄/注冊(cè) 后綁定用戶操作 * * @author Administrator * */ public class Cilent { public static void main(String[] args) { try { Socket socket = new Socket("127.0.0.1", 11536); Menu(socket); } catch (IOException e) { e.printStackTrace(); } } private static void Menu(Socket socket) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter printwriter = null; OutputStream outputStream = null; BufferedReader bufferedReader = null; String choice; do { System.out.println("請(qǐng)您選擇:1.老用戶立即登錄 2.新用戶注冊(cè)即玩\n" + "請(qǐng)輸入正確的數(shù),輸入0退出系統(tǒng)"); choice = sc.next(); System.out.println(choice); // 先傳入玩家的操作選項(xiàng) if (Integer.parseInt(choice) > 0 && Integer.parseInt(choice) < 3) { outputStream = socket.getOutputStream(); byte[] by = choice.getBytes(); outputStream.write(by, 0, by.length); outputStream.flush(); // socket.shutdownOutput(); } printwriter = new PrintWriter(outputStream); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println(bufferedReader.readLine()); switch (choice) { case "0": System.exit(0); break; case "1": ClientLogin(printwriter,sc); break; case "2": ClientRegist(printwriter);// 注冊(cè) ClientLogin(printwriter,sc); break; } } while (Integer.parseInt(choice) > 3 || Integer.parseInt(choice) < 1); while (true) { //登錄完成! //獲取服務(wù)器傳來(lái)的消息! System.out.println("請(qǐng)選擇:1.單抽過(guò)過(guò)癮!2.10連抽任性 0.退出"); String choiceCards = sc.next(); if ("0".equals(choiceCards)) { socket.close(); System.exit(0); } printwriter.println(choiceCards); printwriter.flush(); String str = bufferedReader.readLine(); Logger logger = Logger.getLogger(Client.class); logger.info(str); System.out.println(str); } } /** * 客戶端用戶注冊(cè)//注冊(cè),并將對(duì)象通過(guò)對(duì)象寫出到網(wǎng)絡(luò)流中 * * @param socket * @throws IOException */ private static void ClientRegist(PrintWriter printwriter) throws IOException { UsersDao uersDao = new UsersDao(); Users u = uersDao.UserRegister(); printwriter.println(u); printwriter.flush(); // socket.shutdownOutput(); } private static void ClientLogin(PrintWriter printwriter,Scanner sc){ String name = null; int age = 0 ; while (true) { try { System.out.println("請(qǐng)輸入昵稱"); name = sc.next(); System.out.println("請(qǐng)輸入年齡"); age = sc.nextInt(); break; } catch (Exception e) { System.err.println("您的輸入不合法,請(qǐng)重新輸入"); e.printStackTrace(); } } String checkstr = "Name="+name+":Age="+age; //將字符串傳入網(wǎng)絡(luò)流后對(duì)服務(wù)器的文件進(jìn)行判斷 printwriter.println(checkstr); printwriter.flush(); } }
Server端(多線程)
package SocketS; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * 服務(wù)器 需要完成登錄校驗(yàn) ,注冊(cè)校驗(yàn) 以及游戲抽卡 * @author Administrator * */ public class Server implements Runnable { Socket socket = null; public Server(Socket socket) { super(); this.socket = socket; } @Override public void run() { ServerDao serverDao = new ServerDao(); try { serverDao.Menu(socket); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(11536); } catch (IOException e1) { e1.printStackTrace(); } try { while (true) { socket = serverSocket.accept(); new Thread(new Server(socket)).start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
ServerDao
package SocketS; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; import Cards.Cards; import Cards.CardsDao; import User.UsersDao; /** * 服務(wù)的操作 * * @author Administrator * */ public class ServerDao { UsersDao ud = new UsersDao(); File file = new File("D:\\Users.txt"); /** * 服務(wù)器接收Client發(fā)來(lái)的注冊(cè)信息。并且將用戶信息保存到文件中 * 暗 * @param socket * @return * @throws IOException * @throws ClassNotFoundException */ public boolean isRegistCheck(Scanner sc) throws IOException, ClassNotFoundException { String userStr = getObjectFromSocket(sc); // 將讀取的User對(duì)象寫入文件中 PrintWriter printWriter = new PrintWriter(new FileOutputStream(file,true)); printWriter.println(userStr); printWriter.flush(); return true; } /** * 檢查登錄信息是否正確 ,未完成!?。? * * @param socket * @return 返回登錄信息是否正確 * @throws IOException * @throws ClassNotFoundException */ public boolean isLoginCheck(Scanner sc) throws IOException, ClassNotFoundException { String userStr = getObjectFromSocket(sc); BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); String str = bufferedReader.readLine(); while (str != null) { if (str.contains(userStr)) { return true; } str = bufferedReader.readLine(); } return false; } /** * 獲取客戶端提供的Users對(duì)象 * @param socket 傳入的對(duì)應(yīng)客戶端網(wǎng)絡(luò)套接字 * @param reader 已經(jīng)包裝的BufferedReader 類獲取了網(wǎng)絡(luò)輸入流 * @return 返回Users對(duì)象 * @throws IOException * @throws ClassNotFoundException */ private String getObjectFromSocket(Scanner sc) throws IOException, ClassNotFoundException { // 讀取網(wǎng)絡(luò)流,獲取客戶端提供的Users對(duì)象 String str = null; System.out.println(sc.hasNextLine()); while (sc.hasNextLine()) { str = sc.nextLine(); return str; } return str; } private String getCards(String choiceCards, PrintWriter printWriter){ CardsDao cardsDao = new CardsDao(); if ("1".equals(choiceCards)) { Cards card = cardsDao.once(); return card.getName(); }else if ("2".equals(choiceCards)) {//10連 Cards[] cardsArr = cardsDao.tenTimes(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < cardsArr.length; i++) { sb.append(cardsArr[i].getName()+" "); } return sb.toString(); } return null; } /** * 服務(wù)器接收用戶傳來(lái)的操作信息提供菜單 * * @param socket * @throws IOException * @throws ClassNotFoundException */ public void Menu(Socket socket) throws IOException, ClassNotFoundException { // 先處理玩家發(fā)來(lái)的注冊(cè)登錄選項(xiàng) InputStream in = socket.getInputStream(); Scanner sc = new Scanner(in); byte[] by = new byte[10]; in.read(by, 0, by.length); String str = new String(by); String str1 = "1"; String str2 = "2"; System.out.println(str); str =str.trim(); PrintWriter printWriter = new PrintWriter(socket.getOutputStream()); boolean flag = true; if (str1.equals(str)) { // 登錄 printWriter.println("請(qǐng)輸入登錄信息"); printWriter.flush(); flag= isLoginCheck(sc); } else if (str2.equals(str)) {// printWriter.println("請(qǐng)輸入注冊(cè)信息"); printWriter.flush(); isRegistCheck(sc); flag= isLoginCheck(sc); } if (flag) { System.out.println("歡迎登錄"); }else { System.out.println("請(qǐng)您注冊(cè)后再登錄"); System.exit(0); } while (true) { //獲取玩家的選擇抽卡方式 by = new byte[10]; in.read(by, 0, by.length); String choiceCards = new String(by); choiceCards = choiceCards.trim(); //調(diào)取抽卡游戲: String cardsStr = getCards(choiceCards, printWriter); printWriter.println(cardsStr); printWriter.flush(); } } }
抽卡的具體實(shí)現(xiàn)(比較簡(jiǎn)單)
package Cards; import java.util.Random; /** * 抽卡類 * @author Administrator * */ public class CardsDao { public Cards once(){ double d = Math.random(); if (d>0 && d<=0.01) { return new NvWa(); }else if (d>0.01 && d<=0.1) { return new Crafty(); }else if(d>0.1){ Random random = new Random(); switch (random.nextInt(3)) { case 0: return new HeavenlyHound(); case 1: return new BlackFengHuang(); case 2: return new NineFox(); default: break; } } return null; } public Cards[] tenTimes(){ Cards[] cardsArr = new Cards[10]; for (int i = 0; i < cardsArr.length; i++) { Cards card = once(); cardsArr[i] = card; } return cardsArr; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)抽獎(jiǎng)功能
- Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法
- Java實(shí)現(xiàn)多用戶注冊(cè)登錄的幸運(yùn)抽獎(jiǎng)
- java實(shí)現(xiàn)砸金蛋抽獎(jiǎng)功能
- Java實(shí)現(xiàn)簡(jiǎn)單抽獎(jiǎng)功能界面
- java實(shí)現(xiàn)抽獎(jiǎng)概率類
- 基于Java實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)
- 簡(jiǎn)單實(shí)現(xiàn)java抽獎(jiǎng)系統(tǒng)
- Java抽獎(jiǎng)算法第二例
- 純java代碼實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)
相關(guān)文章
SSM框架整合之Spring+SpringMVC+MyBatis實(shí)踐步驟
大家都知道Spring是一個(gè)輕量級(jí)的控制反轉(zhuǎn)(IoC)和面向切面(AOP)的容器框架,本文主要介紹三大框架的整合包含spring和mybatis的配置文件,還有spring-mvc的配置文件的詳細(xì)介紹,通過(guò)項(xiàng)目實(shí)踐步驟給大家詳細(xì)介紹,感興趣的朋友一起看看吧2021-06-06Java實(shí)戰(zhàn)之小蜜蜂擴(kuò)音器網(wǎng)上商城系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)簡(jiǎn)單的小蜜蜂擴(kuò)音器網(wǎng)上商城系統(tǒng),文中采用到的技術(shù)有JSP、Servlet?、JDBC、Ajax等,感興趣的可以動(dòng)手試一試2022-03-03java編寫一個(gè)花名隨機(jī)抽取器的實(shí)現(xiàn)示例
這篇文章主要介紹了java編寫一個(gè)花名隨機(jī)抽取器的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08jeefast和Mybatis實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的示例代碼
這篇文章主要介紹了jeefast和Mybatis實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10java實(shí)現(xiàn)微博后臺(tái)登錄發(fā)送微博
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微博后臺(tái)登錄發(fā)送微博的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Spring?Boot?Actuator管理日志的實(shí)現(xiàn)
本文主要介紹了Spring?Boot?Actuator管理日志的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07如何基于mybatis框架查詢數(shù)據(jù)庫(kù)表數(shù)據(jù)并打印
這篇文章主要介紹了如何基于mybatis框架查詢數(shù)據(jù)庫(kù)表數(shù)據(jù)并打印,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11