使用java實(shí)現(xiàn)猜拳小游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)猜拳小游戲的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)下圖要求
public class User { private String u_name; private int u_score; public User() { super(); } public User(String name, int score) { super(); this.u_name = name; this.u_score = score; } public String getName() { return u_name; } public void setName(String name) { this.u_name = name; } public int getScore() { return u_score; } public void setScore(int score) { this.u_score = score; } /** * 出拳方法 * @param choice 選擇的數(shù)字代表出拳(1:石頭2:剪刀3:布) * @return str 返回你所選擇的出拳 */ public String chuQuan(int choice){ String str = ""; switch (choice) { case 1: str = "石頭"; break; case 2: str = "剪刀"; break; case 3: str = "布"; break; default: System.out.println("未知錯誤"); break; } return str; } }
public class Computer { private String c_name; private int c_score; public String getName() { return c_name; } public void setName(String name) { this.c_name = name; } public int getScore() { return c_score; } public void setScore(int score) { this.c_score = score; } /** * 出拳方法 * @param choice 選擇的數(shù)字代表出拳(1:石頭2:剪刀3:布) * @return str 返回你所選擇的出拳 */ public String chuQuan(int choice){ String str = ""; switch (choice) { case 1: str = "石頭"; break; case 2: str = "剪刀"; break; case 3: str = "布"; break; default: System.out.println("未知錯誤"); break; } return str; } }
import java.util.Scanner; public class Game { Scanner input = new Scanner(System.in); private User user; private Computer computer; private int count; private int c_score; private int u_score; //初始化方法 public void init(){ user = new User(); computer = new Computer(); System.out.println("-----------------歡迎進(jìn)入游戲世界------------------"); System.out.println("\t **************************"); System.out.println("\t\t** 猜拳,開始 **"); System.out.println("\t **************************"); System.out.println(); System.out.println("出拳規(guī)則:1.石頭 2.剪刀 3.布"); System.out.print("請選擇對方角色:(1:曹操 2:孫權(quán) 3:劉備):"); int key = input.nextInt(); switch (key) { case 1: computer.setName("曹操"); break; case 2: computer.setName("孫權(quán)"); break; case 3: computer.setName("劉備"); break; default: System.out.println("非法輸入..."); break; } System.out.print("請輸入你的姓名:"); user.setName(input.next()); System.out.println(user.getName()+" VS "+computer.getName()); begin(); } //是否開始執(zhí)行 循環(huán)執(zhí)行直到輸入n結(jié)束 public void begin(){ System.out.print("要開始嗎(y/n):"); // boolean falg = true; String str = input.next(); if(str.equals("y")){ while(true){ score(); System.out.print("是否開始下一輪:(y/n)"); String str1 = input.next(); count++; if(str1.equals("y")){ }else{ // falg = false; break; } } } show(); } //人和機(jī)器出拳并判斷勝負(fù) 此處計算比賽次數(shù) 雙方得分 public void score(){ System.out.print("請出拳:"); int choice1 = input.nextInt(); String str1 = user.chuQuan(choice1); int choice2 = (int)(Math.random()*3+1); String str2 = computer.chuQuan(choice2); System.out.println("你出拳"+str1); System.out.println(computer.getName()+"出拳"+str2); if(choice1 == choice2){ System.out.println("結(jié)果:平局"); }else if(choice2-choice1==-1||choice2-choice1==2){ System.out.println("結(jié)果:"+computer.getName()+"獲勝..."); c_score++; computer.setScore(c_score); }else if(choice1-choice2==-1||choice1-choice2==2){ System.out.println("結(jié)果:恭喜你,你獲勝..."); u_score++; user.setScore(u_score); } } //顯示比賽結(jié)果并比較得得出最后勝負(fù) public void show(){ System.out.println("--------------------------------"); System.out.println(user.getName()+" VS "+computer.getName()); System.out.println("對戰(zhàn)次數(shù):"+count+"\n\n"); System.out.println("姓名\t得分"); System.out.println(user.getName()+"\t"+user.getScore()); System.out.println(computer.getName()+"\t"+computer.getScore()+"\n"); if(user.getScore()>computer.getScore()){ System.out.println("結(jié)果:恭喜恭喜"); }else if(user.getScore()<computer.getScore()){ System.out.println("結(jié)果:再接再厲"); }else{ System.out.println("結(jié)果:平局"); } System.out.println("--------------------------------"); } }
測試類
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Game game = new Game(); game.init(); } }
這樣猜拳小游戲就實(shí)現(xiàn)了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Hibernate對象狀態(tài)之間的神奇轉(zhuǎn)換
這篇文章主要介紹了淺談Hibernate對象狀態(tài)之間的神奇轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法
這篇文章主要介紹了SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Java實(shí)現(xiàn)的漢語拼音工具類完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的漢語拼音工具類,結(jié)合完整實(shí)例形式分析了java基于pinyin4j包實(shí)現(xiàn)編碼轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Springmvc數(shù)據(jù)格式化原理及代碼案例
這篇文章主要介紹了Springmvc數(shù)據(jù)格式化原理及代碼案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10Struts2返回json格式數(shù)據(jù)代碼實(shí)例
這篇文章主要介紹了Struts2返回json格式數(shù)據(jù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Java實(shí)現(xiàn)英文句子中的單詞順序逆序輸出的方法
這篇文章主要介紹了Java實(shí)現(xiàn)英文句子中的單詞順序逆序輸出的方法,涉及java字符串遍歷、判斷、截取、輸出等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01springboot使用定時器@Scheduled不管用的解決
這篇文章主要介紹了springboot使用定時器@Scheduled不管用的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12JMeter參數(shù)化4種實(shí)現(xiàn)方式(小結(jié))
參數(shù)化是自動化測試腳本的一種常用技巧,可將腳本中的某些輸入使用參數(shù)來代替,JMeter提供了多種參數(shù)化方式,下面就其中常用的4種展開闡述,感興趣的可以來了解一下2021-12-12java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12