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

