Java面向对象牛刀小试 --石头剪刀布

题目 : 编写一个程序,实现人和电脑的猜拳比赛,要求:三局两胜

  • 首先写一个 Person 类
package test1.java.xiaoyu;

public class Person {
    //定义 score 属性
    public static int score = 0;

    /**
     * addScore() 用来记录 Person 的分数
     */
    public static void addScore(){
        score++;
    }
}

  • 再写一个 Computer 类
package test1.java.xiaoyu;

public class Computer {
    //定义 score、index 属性
    public static int score = 0;
    public static int index;

    public static void addScore(){
        score++;
    }

    /**
     * computerPlay() 用 index 来控制 Computer 的出拳
     * @return
     */
    public static String computerPlay(){
        if(index == 0) {
            return "石头";
        }else if(index == 1) {
            return "剪刀";
        }else if(index == 2){
            return "布";
        }
        return " ";
    }
}

  • 来一个 Game 类,实现人和电脑的对决
package test1.java.xiaoyu;

import java.util.Random;
import java.util.Scanner;

public class Game implements Comparable{
    //利用组合方式,创建 Person、Computer 对象
    Person person = new Person();
    Computer computer = new Computer();

    /**
     * start() 方法,启动猜拳
     */
    public static void start(){
        System.out.println("开始");
        PersonPlay();
        while(true) {
            System.out.println("是否继续?");
            Scanner scanner = new Scanner(System.in);
            String out = scanner.nextLine();
            if (out.equals("继续")) {
                System.out.println("             ");
                PersonPlay();
            } else {
                return;
            }
        }
    }

    /**
     * 三次猜拳的比较总结果
     */
    private static void PersonPlay() {
        for (int i = 0; i < 3; i++) {
            compare1();
        }
        Game gameScore = new Game();
        int j = gameScore.compareTo(gameScore);
        System.out.println("Person:"+Person.score+"  "+"Computer:"+Computer.score);
        if(j == 0){
            System.out.println("最终平局");
        }
        if(j > 0){
            System.out.println("最终Person获胜");
        }
        if(j < 0){
            System.out.println("最终Computer获胜");
        }
    }

    /**
     * 对一次猜拳输赢进行判断
     * @return
     */
    public static int compare1(){
            Scanner in = new Scanner(System.in);
            String string = in.nextLine();
            Random random = new Random();
            Computer.index = random.nextInt(2);

            //第一种判断方法
           /*if(string.equals(Computer.computerPlay())) {
                System.out.println("平局");
            }else if(string.equals("石头") && Computer.computerPlay().equals("剪刀") ||
                    string.equals("剪刀") && Computer.computerPlay().equals("布") ||
                    string.equals("布") && Computer.computerPlay().equals("石头")){
                Person.addScore();
                System.out.println("Person 获胜");
            }else{
                Computer.addScore();
                System.out.println("Computer 获胜");
                return Computer.score;
            }*/
            //第二种判断方法
            if (string.equals("石头") && Computer.index == 0) {
                System.out.println("平局");
            }else if(string.equals("剪刀") && Computer.index == 1){
                System.out.println("平局");
            }else if(string.equals("布") && Computer.index == 2){
                System.out.println("平局");
            }else if(string.equals("石头") && Computer.index == 1){
                System.out.println("Person获胜");
                Person.addScore();
            }else if(string.equals("石头") && Computer.index == 2){
                System.out.println("Computer获胜");
                Computer.addScore();
            }else if(string.equals("剪刀") && Computer.index == 0){
                System.out.println("Computer获胜");
                Computer.addScore();
            }else if(string.equals("剪刀") && Computer.index == 2){
                System.out.println("Person获胜");
                Person.addScore();
            }else if(string.equals("布") && Computer.index == 2){
                System.out.println("Computer获胜");
                Computer.addScore();
                return Computer.score;
            }else if(string.equals("布") && Computer.index == 0){
                System.out.println("Person获胜");
                Person.addScore();
            }
            return Person.score;
        }

    /**
     * 重写 compareTo() 方法,对 Person 和 Computer 的 分数进行总比较
     * @param o
     * @return
     */
    @Override
    public int compareTo(Object o) {
        compare1();
        return Person.score - Computer.score;
    }
}







  • 测试类
package test1.java.xiaoyu;

public class GameTest {
    public static void main(String[] args){
        Game.start();
    }
}

  • 测试结果
    在这里插入图片描述
    在这里插入图片描述
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值