Java實(shí)現(xiàn)人機(jī)猜拳游戲
本文實(shí)例為大家分享了Java實(shí)現(xiàn)人機(jī)猜拳游戲的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn):
User類(lèi)
public class User { private String name; private int score=0; private int num; public String GetName() { return this.name; } public void SetName(String name) { this.name=name; } public int GetScore() { return this.score; } public void SetScore(int score) { this.score+=score; } }
Computer類(lèi)
public class Computer { private String name; private int score=0; private int num; public String GetName() { return this.name; } public void SetName(String name) { this.name=name; } public int RandNums() { int n; n=(int)(Math.random()*3)+1;// 返回1到3的隨機(jī)整數(shù)。 return n; } public int GetScore() { return this.score; } public void SetScore(int score) { this.score+=score; }}
Gamemanager類(lèi)
import java.util.Scanner; public class GameManager { public static void main(String[] args) { Scanner input=new Scanner(System.in);//創(chuàng)建一個(gè)鍵盤(pán)掃描類(lèi)對(duì)象 User user=new User(); Computer computer=new Computer(); int vsNums=0; System.out.println('出拳游戲規(guī)則:1、剪刀,2、石頭,3、布'); System.out.println('請(qǐng)選擇對(duì)方角色(1、劉備,2、孫權(quán),3、曹操)'); int n=input.nextInt(); //輸入整型 switch(n) { case 1: computer.SetName('劉備'); break; case 2: computer.SetName('孫權(quán)'); break; case 3: computer.SetName('曹操'); break; } System.out.println('請(qǐng)輸入你的姓名'); String name=input.next(); //輸入字符串型 user.SetName(name); System.out.println(user.GetName()+' '+'VS'+' '+computer.GetName()); String flag='y'; while(flag.equals(flag)) { System.out.println('要開(kāi)始嗎y/n'); String yOrn=input.next(); //輸入字符串型 if(yOrn.equals('y')) { vsNums++; System.out.println('請(qǐng)出拳:1、剪刀,2、石頭,3、布(輸入數(shù)字)'); int nums=input.nextInt(); //輸入整型 switch(nums) { case 1: System.out.println('你出拳:'+'剪刀'); break; case 2: System.out.println('你出拳:'+'石頭'); break; case 3: System.out.println('你出拳:'+'布'); break; } int rand=computer.RandNums(); switch(rand) { case 1: System.out.println(computer.GetName()+'出拳:'+'剪刀'); break; case 2: System.out.println(computer.GetName()+'出拳:'+'石頭'); break; case 3: System.out.println(computer.GetName()+'出拳:'+'布'); break; } if(nums==1 && rand==3 || nums==2 && rand==1 || nums==3 && rand==2) { System.out.println('恭喜,你贏了'); user.SetScore(1); } else if(nums==rand) { System.out.println('平手了'); } else { System.out.println('很遺憾,你輸了'); computer.SetScore(1); } } else { System.out.println(computer.GetName()+' '+'VS'+' '+user.GetName()); System.out.println('對(duì)戰(zhàn)次數(shù):'+vsNums); System.out.println('姓名t得分'); System.out.println(user.GetName()+'t'+user.GetScore()); System.out.println(computer.GetName()+'t'+computer.GetScore()); if(user.GetScore()>computer.GetScore()) { System.out.println('恭喜,恭喜'); } else { System.out.println('繼續(xù)加油'); } break; } } }}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專(zhuān)題,分享給大家:
C++經(jīng)典小游戲匯總
python經(jīng)典小游戲匯總
python俄羅斯方塊游戲集合
JavaScript經(jīng)典游戲 玩不停
java經(jīng)典小游戲匯總
javascript經(jīng)典小游戲匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python用pyecharts實(shí)現(xiàn)地圖數(shù)據(jù)可視化2. python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例3. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法4. 詳解Python 3.10 中的新功能和變化5. npm下載慢或下載失敗問(wèn)題解決的三種方法6. ASP編碼必備的8條原則7. Python中re模塊的常用方法總結(jié)8. python基于opencv批量生成驗(yàn)證碼的示例9. ASP錯(cuò)誤捕獲的幾種常規(guī)處理方式10. 如何用python開(kāi)發(fā)Zeroc Ice應(yīng)用
