十二章 练习

public class TestGame  
{  
    public static void main(String[] args)  
    {  
        Game game=new Game();  
        game.satrtGame();  
    }  
}  





package ch12;  
  
import java.util.Random;  
  
public class Computer  
{  
  
    public String name;  
    public int score;  
  
    public int showFist()  
    {  
  
        Random r = new Random();  
        int num = r.nextInt(3) + 1;  
        switch (num)  
        {  
        case 1:  
            System.out.println(name+"出的是剪刀");  
            break;  
        case 2:  
            System.out.println(name+"出的是石头");  
            break;  
        case 3:  
            System.out.println(name+"出的是布");  
            break;  
        }  
        return num;  
    }  
  
}  






public class Person  
{  
    public String name;  
    public int score;  
    /** 
     * 用户出拳 
     *  
     * @return 返回用户出拳是什么 1 .剪刀 2.石头 3.布 
     */  
    public int showFirst()  
    {  
        Scanner input = new Scanner(System.in);  
        int ch = 0;  
        boolean b = true;  
        while (b)  
        {  
            System.out.println("1.剪刀\t2.石头\t3.布");  
            ch = input.nextInt();  
            switch (ch)  
            {  
            case 1:  
                System.out.println("您出的是剪刀");  
                b = false;  
                break;  
            case 2:  
                System.out.println("您出的是石头");  
                b = false;  
                break;  
            case 3:  
                System.out.println("您出的是布");  
                b = false;  
                break;  
            default:  
                System.out.println("输入错误,请输入1--3 任意整数");  
                b = true;  
                break;  
            }  
  
        }  
  
        return ch;  
    }  
  
}  





public class Game  
{  
    private Person person;// 人玩家  
    private Computer computer;// 电脑玩家  
    private int count; // 对战次数  
    Scanner input = new Scanner(System.in);  
  
    private void init()  
    {  
        person = new Person();  
        computer = new Computer();  
    }  
  
    public void satrtGame()  
    {  
        init();  
        int p = 0;// 代表用户出拳  
        int c = 0;// 代表电脑出拳  
  
        System.out.println("************************");  
        System.out.println("******* 猜拳    开始*******");  
        System.out.println("************************");  
        System.out.println("出拳规则1.剪刀2.石头3.布");  
        System.out.println("请选择对方角色(1.刘备2.孙权3.曹操)");  
        switch (input.nextInt())  
        {  
        case 1:  
            computer.name = "刘备";  
            break;  
        case 2:  
            computer.name = "孙权";  
            break;  
        case 3:  
            computer.name = "曹操";  
            break;  
        }  
        System.out.println("请输入您的姓名");  
        person.name = input.next();  
        System.out.println(person.name + "VS" + computer.name + "对战");  
        System.out.println("要开始吗y/n");  
        char a = input.next().charAt(0);  
        if (a == 'y')  
        {  
            char b = 'y';  
            while (b == 'y')  
            {  
                System.out.print("请出拳");  
                p = person.showFirst();  
                c = computer.showFist();  
                if (p == c)  
                {  
                    System.out.println("和局");  
                    // 人赢  
                } else if (p == 1 && c == 3 || p == 2 && c == 1 || p == 3 && c == 2)  
                {  
                    System.out.println("恭喜你赢了");  
                    person.score += 1;// person.score=person.score+1;  
                } else  
                {  
                    System.out.println("你输了");  
                    computer.score += 1;  
                }  
                count++;  
                System.out.println("是否开始下一轮y/n");  
                b = input.next().charAt(0);  
            }  
            showResult();// 显示对战结果  
                              
        } else  
        {  
            System.out.println("游戏退出");  
        }  
    }  
    // 显示对战结果  
    public void showResult(){  
          
        System.out.println(person.name+"VS"+computer.name);  
        System.out.println("对战次数"+count);  
        System.out.println();  
          
        System.out.println("姓名\t得分");  
        System.out.println(person.name+"\t"+person.score);  
        System.out.println(computer.name+"\t"+computer.score);  
        if(person.score>computer.score){  
            System.out.println("你真厉害赢了");  
              
        }else if(person.score<computer.score){  
            System.out.println("你真笨,输了");  
        }else{  
            System.out.println("平局");  
              
        }  
    }  
  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值