java項(xiàng)目實(shí)現(xiàn)猜拳小游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)猜拳小游戲的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目名稱
猜拳小游戲
項(xiàng)目描述
玩家與電腦進(jìn)行猜拳游戲,玩家行為采用輸入方式,電腦行為采用隨機(jī)形式。
代碼實(shí)現(xiàn)
測試類
public class Test {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
主類:實(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("開始游戲:");
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("是否開始新游戲:");
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;
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java如何使用正則表達(dá)式從字符串中提取數(shù)字
這篇文章主要介紹了Java如何使用正則表達(dá)式從字符串中提取數(shù)字問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)
SpringBoot項(xiàng)目經(jīng)常將連接數(shù)據(jù)庫的密碼明文放在配置文件里,安全性就比較低一些,尤其在一些企業(yè)對(duì)安全性要求很高,因此我們就考慮如何對(duì)密碼進(jìn)行加密,文中給大家介紹加密的兩種方式,感興趣的朋友一起看看吧2019-10-10
ReentrantLock條件變量使多個(gè)線程順序執(zhí)行
這篇文章主要為大家介紹了ReentrantLock條件變量使多個(gè)線程順序執(zhí)行,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
K均值聚類算法的Java版實(shí)現(xiàn)代碼示例
這篇文章主要介紹了K均值聚類算法的Java版實(shí)現(xiàn)代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
Java數(shù)據(jù)結(jié)構(gòu)之線性表
線性表是其組成元素間具有線性關(guān)系的一種數(shù)據(jù)結(jié)構(gòu),對(duì)線性表的基本操作主要有,獲取元素,設(shè)置元素值,遍歷,插入,刪除,查找,替換,排序等。而線性表可以采用順序儲(chǔ)存結(jié)構(gòu)和鏈?zhǔn)絻?chǔ)存結(jié)構(gòu),本節(jié)主要講解順序表、單鏈表以及雙鏈表的各種基本操作。2017-03-03
Java里的static在Kotlin里如何實(shí)現(xiàn)
這篇文章主要介紹了Java里的static在Kotlin里如何實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
基于Java實(shí)現(xiàn)一個(gè)簡單的單詞本Android App的實(shí)踐
本文基于Java實(shí)現(xiàn)了一個(gè)簡單的單詞本安卓app,用的是SQLite數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

