服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现和电脑玩剪刀石头布游戏

Java实现和电脑玩剪刀石头布游戏

2022-01-04 00:40湬䶪齾巅Jiuchayadian Java教程

这篇文章主要为大家详细介绍了Java实现和电脑玩剪刀石头布游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

剪刀石头布游戏大家都玩过吧,今天分享Java实现和电脑玩剪刀石头布游戏的具体代码,供大家参考,具体内容如下

游戏总类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;
public class Game {
    Scanner input = new _Scanner(System.in);
    String judge;
    int num1;//人返回的数字
    int num2;//机器返回的数字
    Computer computer = new Computer();
    Person person = new Person();
    boolean flage=false;
    int computerCount=0;
    int personCount=0;
    int pingCount=0;
    int count=0;
    public Game( ){
    }
    public boolean  start(String computerName,String inputName) {
        System.out.println("要开始了吗?(y/n):");
        judge = input.next();
        if (judge.equals("y")) {
            do{
                count+=1;
                flage=false;
                num1 = person.hand();
                System.out.print(computerName+"出拳:");
                num2 = computer.hand();
                if((num1==1&&num2==1)||(num1==2&&num2==2)||(num1==3&&num2==3)){
                    System.out.println("平局");
                    System.out.println("运气平分秋色!!!!");
                    System.out.println("出拳规则:1.剪刀2.石头3.布");
                    pingCount+=1;
                }else if((num1==1&&num2==3)||(num1==2&&num2==1)||(num1==3&&num2==2)){
                    System.out.println("恭喜你,你赢了!!!!!!");
                    personCount+=1;
                }else if((num2==1&&num1==3)||(num2==2&&num1==1)||(num2==3&&num1==2)){
                    System.out.println("运气真不好,你输了!!!!!!");
                    computerCount+=1;
                }
                System.out.println("\n是否进行下一轮?(y/n):");
                judge = input.next();
                switch (judge){
                    case "y":
                        flage=true;
                        break;
                    case "n":
                        System.out.println(computerName+"\t\t"+"vs\t\t"+inputName );
                        System.out.println("对战次数:"+count);
                        System.out.println("姓名\t\t"+"得分");
                        System.out.println(inputName +"\t\t"+personCount);
                        System.out.println(computerName+"\t\t"+ computerCount);
                        System.out.println("平局\t\t"+ pingCount);
                        System.out.println("结果: \t\t呵呵 \t\t笨笨\t\t 下次加油吧");
                        break;
                }
            }while (flage);
        } else if (judge.equals("n")) {
        }
        return flage;
    }
}

人类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner;
public class Person {
    Scanner input=new Scanner(System.in);
 
    String personName;
    int personNum;
    int score;
    int num;
    //输入名字
    public String personName(String iputName){
        this.personName=iputName;
        return this.personName;
    }
    //出拳
    public int hand(){
        System.out.print("请出拳:1.剪刀\t2.石头\t3.布(请输入相应的数字):");
        num=input.nextInt();
        System.out.print("你出拳:");
        if(num==1){
            System.out.println("剪刀");
        }else if(num==2){
            System.out.println("石头");
        }else if(num==3){
            System.out.println("布");
        }
        return num;
    }
}

电脑类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Random;
import java.util.Scanner;
public class Computer {
    Scanner input = new Scanner(System.in);
    String name;
    Random random = new Random();
 
    public String computerName( int num) {
        if (num == 1) {
            name = "刘备";
        } else if (num == 2) {
             name = "孙权";
        } else if (num == 3) {
            name = "曹操";
        }
        return name;
    }
    //出拳
    public int hand(){
        int randomNum = random.nextInt(3) % (3 - 1 + 1) + 1;
        if(randomNum==1){
            System.out.println("剪刀");
        }else if(randomNum==2){
            System.out.println("石头");
        }else if(randomNum==3){
            System.out.println("布");
        }
        return randomNum;
    }
}

main()函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;
public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        Computer computer=new Computer();
        Person person=new Person();
        Game game=new Game();
        System.out.println("************************************");
        System.out.println("\t\t\t猜拳\t开始");
        System.out.println("************************************");
        System.out.println("出拳规则:1.剪刀2.石头3.布");
        System.out.println("请选择对方角色:1.刘备\t2.孙权\t3.曹操");
        System.out.print("请输入对战的角色:");
        int num = input.nextInt();
        String computerName= computer.computerName(num);
        System.out.print("请输入你的名字:");
        String inputName=input.next();
        String personName= person.personName(inputName);
        System.out.println(computerName+"\t\tvs\t"+personName);
        game.start(computerName,inputName) ;
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/m0_55004108/article/details/120244319

延伸 · 阅读

精彩推荐