猜拳游戏

猜拳入口

public class Mora {
    public static void main(String[] args) {
        int Scoresum = 0;
        for (int a = 1;true;a++){
            Mora mora = new Mora();
            System.out.println("第"+a+"局");
            Scoresum = mora.scoreSum(Scoresum);
            System.out.println(Scoresum);
        }




    }
    public int scoreSum(int Scoresum){
        Person per1 = new Person();
        Robot rob1 = new Robot();
        per1.sayToNum();
        per1.getPersonNum();
        int a = rob1.getRobotNum();
        int b = per1.getPersonNum();
        VS vs1 = new VS();
        int c = vs1.vs(b,a);
        Scoresum = c + Scoresum;
        return Scoresum;
    }

}

对比计分

//把人和机器猜拳比对计分

public class VS{
    private int score = 0;
    public int vs(int a,int b){
        if(a == 1&b == 3){
            score++;
        }else if(a == 1&b == 2){
            score--;
        }
        if(a == 2&b == 1){
            score++;
        }else if(a == 2&b == 3){
            score--;
        }
        if(a == 3&b == 2){
            score++;
        }else if(a == 3&b == 1){
            score--;
        }
        return score;
    }
}

建立猜拳人的类



import java.util.Scanner;


    //剪刀 = 1, 石头 = 2, 布 = 3;

public class Person{
    //人猜拳对应的数字

    private int personNum = 0;
        public int getPersonNum() {
            return personNum;
        }
        //人说的文字

        private String personSay;

    //文字转换为数字

    public int sayToNum() {
        System.out.println("剪刀石头步");
        Scanner scan = new Scanner(System.in);
        personSay = scan.next();
        if (personSay.equals("剪刀")) {
            personNum = 1;

        }
        else if (personSay.equals("石头")){
            personNum = 2;

        }
        else if (personSay.equals("布")){
            personNum = 3;

        }
        else {
            System.out.println("你玩的是同一款游戏吗?");
            sayToNum();
        }
        return personNum;
    }
}

机器人类

import java.util.Random;
//机器人随机生成(剪刀、石头、布)

public class Robot{
    Random random = new Random();
    private int robotNum;
    public int getRobotNum (){
        robotNum = random.nextInt(3)+1;
        switch(robotNum){
            case 1:
                System.out.println("我出剪刀");
                break;
            case 2:
                System.out.println("我出石头");
                break;
            default:
                System.out.println("我出布");
                break;
        }
        return robotNum;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值