石头,剪刀,布游戏

import java.util.Scanner;
import java.util.InputMismatchException;
 * 游戏:石头,剪刀,布
 * 编写可以玩最流行的剪刀=石头=布游戏程序
 * 程序提示电脑随机产生一个数,这个数为0,1或者2,分别表示石头,剪刀和布。
 * 程序提示用户输入0,1或者2,然后显示一个消息,表明用户 和计算机谁赢了游戏,谁输了游戏,或者打成平手。
 * 同时直到用户或者计算机连续赢两次以上,程序终止
 */
public class Game {
    int per = 0;//全局变量用来存放电脑和人赢得次数!
    int com = 0;

    public int result(int personInput, int computerInput) {
        if (personInput == computerInput) {
            if (personInput == 0) {
                System.out.println("我出石头,电脑也出石头!");
                System.out.println("我们是平局!!!!");
                per = 0;
                com = 0;
            } else if (personInput == 1) {
                System.out.println("我出剪刀,电脑也出剪刀!");
                System.out.println("我们是平局!!!!");
                per = 0;
                com = 0;
            } else if (personInput == 2) {
                System.out.println("我出布,电脑也出布!");
                System.out.println("我们是平局!!!!");
                per = 0;
                com = 0;
            }

        } else if ((personInput == 0 && computerInput == 1) || (personInput == 1 && computerInput == 2) || (personInput == 2 && computerInput == 0)) {
            if (personInput == 0) {
                System.out.println("我出石头!!");
                System.out.println("电脑出剪刀");
                System.out.println("我赢了  !!!");
                per += 1;
                com = 0;
            } else if (personInput == 1) {
                System.out.println("我出剪刀!!");
                System.out.println("电脑出布");
                System.out.println("我赢了 !!!");
                per += 1;
                com = 0;
            } else if (personInput == 2) {
                System.out.println("我出布!!");
                System.out.println("电脑石头!");
                System.out.println("我赢了 !!!");
                per += 1;
                com = 0;
            }
        } else if ((personInput == 0 && computerInput == 2) || (personInput == 1 && computerInput == 0) || (personInput == 2 && computerInput == 1)) {
            if (personInput == 0) {
                System.out.println("我出石头!!");
                System.out.println("电脑布");
                System.out.println("电脑赢了  !!!");
                com += 1;
                per = 0;
            } else if (personInput == 1) {
                System.out.println("我出剪刀!!");
                System.out.println("电脑出石头!!");
                System.out.println("电脑赢了  !!!");
                com += 1;
                per = 0;
            } else if (personInput == 2) {
                System.out.println("我出布!!");
                System.out.println("电脑出剪刀!!!");
                System.out.println("电脑赢了  !!!");
                com += 1;
                per = 0;
            }
        }
        if (per >= 2) {
            System.out.println("我赢了" + per + "局!!");
            return per;
        } else if (com >= 2) {
            System.out.println("电脑赢了" + com + "局!!");
            return com;
        } else return 0;
    }

    public static class Person {
        public int input() {
            System.out.println("请输入:0(0,代表石头),1(1.代表剪刀),2(2代表布):");
            Scanner sc = new Scanner(System.in);
            int personInput = -1;
            boolean Isend = false;
            while (!Isend) {
                try {
                    personInput = sc.nextInt();
                    Isend = true;
                } catch (InputMismatchException e) {
                    System.out.println("输入错误!请重新输入0(0,代表石头),1(1.代表剪刀),2(2代表布)!!!");
                    sc.nextLine();
                    Isend = false;
                }
            }
            return personInput;
        }
    }

    public static class Computer //电脑随即生成的随机数
    {
        public int random() {
            int h = (int) (Math.random() * 3);//生成0--3之间的随机数(包含0)
            return h;
        }
    }

    public static void main(String[] args) {
        Person person = new Person();
        Computer computer = new Computer();
        Game game = new Game();
        boolean end = false;
        while (!end) {
            int a = game.result(person.input(), computer.random());
            if (a >=2) {
                end = true;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值