小編這次要給大家分享的是java項(xiàng)目如何實(shí)現(xiàn)猜拳小游戲,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
成都創(chuàng)新互聯(lián)是一家企業(yè)級(jí)云計(jì)算解決方案提供商,超15年IDC數(shù)據(jù)中心運(yùn)營(yíng)經(jīng)驗(yàn)。主營(yíng)GPU顯卡服務(wù)器,站群服務(wù)器,多線服務(wù)器托管,海外高防服務(wù)器,大帶寬服務(wù)器,動(dòng)態(tài)撥號(hào)VPS,海外云手機(jī),海外云服務(wù)器,海外服務(wù)器租用托管等。
項(xiàng)目名稱(chēng)
猜拳小游戲
項(xiàng)目描述
玩家與電腦進(jìn)行猜拳游戲,玩家行為采用輸入方式,電腦行為采用隨機(jī)形式。
代碼實(shí)現(xiàn)
測(cè)試類(lèi)
public class Test { public static void main(String[] args) { Game game = new Game(); game.start(); } }
主類(lèi):實(shí)現(xiàn)主方法
public class Game { private People people; private Computer computer; public Game(){ people = new People("zs"); computer = new Computer("computer"); } public void start(){ boolean flag = true; while (flag) { System.out.println("開(kāi)始游戲:"); int count = 0; while (count < 3) { String peopleFist = people.doFist(); String comFist = computer.doFist(); //people贏 if (peopleFist.equals("石頭") && comFist.equals("剪刀") || peopleFist.equals("剪刀") && comFist.equals("布") || peopleFist.equals("布") && comFist.equals("石頭")) { System.out.println(people.getName() + "贏了"); people.addScore(1); } else if (peopleFist.equals("石頭") && comFist.equals("石頭") || peopleFist.equals("剪刀") && comFist.equals("剪刀") || peopleFist.equals("布") && comFist.equals("布")) { System.out.println("平局"); } else if (peopleFist.equals("石頭") && comFist.equals("布") || peopleFist.equals("剪刀") && comFist.equals("石頭") || peopleFist.equals("布") && comFist.equals("剪刀")) { System.out.println(computer.getName() + "贏了"); computer.addScore(1); } count++; } if (people.getScore() > computer.getScore()) { System.out.println(people.getName() + "贏了 " + people.getScore() + ":" + computer.getScore()); } else if (people.getScore() == computer.getScore()) { System.out.println("平局"); } else if (people.getScore() < computer.getScore()) { System.out.println(computer.getName() + "贏了 " + computer.getScore() + ":" + people.getScore()); } System.out.println("是否開(kāi)始新游戲:"); Scanner scanner = new Scanner(System.in); String str = scanner.next(); if (str.equals("否")) { flag = false; }else { people.setScore(); computer.setScore(); } } } }
玩家
public class People { private String name; private int score; public People(String name){ this.name = name; score = 0; } public String getName(){ return name; } public void addScore(int score){ this.score += score; } public int getScore(){ return score; } public int setScore(){ this.score = 0; return score; } public String doFist(){ System.out.println("請(qǐng)出拳:"); Scanner scanner = new Scanner(System.in); String fist = scanner.next(); return fist; } }
電腦
public class Computer { private String name; private int score; public Computer(String name){ this.name = name; score = 0; } public String getName(){ return name; } public void addScore(int score){ this.score += score; } public int getScore(){ return score; } public int setScore(){ this.score = 0; return score; } public String doFist(){ Random random = new Random(); int n = random.nextInt(3); String fist; if(n == 0){ fist = "石頭"; }else if(n == 1){ fist = "剪刀"; }else { fist = "布"; } System.out.println("對(duì)方出的是:"+fist); return fist; } }
看完這篇關(guān)于java項(xiàng)目如何實(shí)現(xiàn)猜拳小游戲的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話,可以把它分享出去給更多人看到。
分享題目:java項(xiàng)目如何實(shí)現(xiàn)猜拳小游戲
網(wǎng)站路徑:http://chinadenli.net/article4/gdedoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站收錄、云服務(wù)器、服務(wù)器托管、域名注冊(cè)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)