java項目實現(xiàn)猜拳小游戲
更新時間:2020年05月27日 09:18:01 作者:Sampson_S
這篇文章主要為大家詳細介紹了java項目實現(xiàn)猜拳小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)猜拳小游戲的具體代碼,供大家參考,具體內容如下
項目名稱
猜拳小游戲
項目描述
玩家與電腦進行猜拳游戲,玩家行為采用輸入方式,電腦行為采用隨機形式。
代碼實現(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)典小游戲實現(xiàn)專題,分享給大家:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
spring?Cloud微服務阿里開源TTL身份信息的線程間復用
這篇文章主要為大家介紹了spring?Cloud微服務中使用阿里開源TTL身份信息的線程間復用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
JAVA統(tǒng)計字符串中某個字符出現(xiàn)次數(shù)的方法實現(xiàn)
本文主要介紹了JAVA統(tǒng)計字符串中某個字符出現(xiàn)次數(shù)的方法實現(xiàn),可以循環(huán)使用String的charAt(int index)函數(shù),具有一定的參考價值,感興趣的可以了解一下2023-11-11
SpringBoot+RabbitMQ實現(xiàn)消息可靠傳輸詳解
消息的可靠傳輸是面試必問的問題之一,保證消息的可靠傳輸主要在生產(chǎn)端開啟?comfirm?模式,RabbitMQ?開啟持久化,消費端關閉自動?ack?模式。本文將詳解SpringBoot整合RabbitMQ如何實現(xiàn)消息可靠傳輸,需要的可以參考一下2022-05-05

