run in this way,   no why,   only for you heart
CSDN博客(点击进入) CSDN
51CTO(点击进入) 51CTO

人机猜拳

用户类

package com.guess;

import java.util.Scanner;

/**
 * 用户类
 * 
 * @author hp
 *
 */
public class Person {
    String name;// 用户民
    int perFist;// 用户出拳号
    int perScore;// 用户得分

    public void showFist() {// 用户出拳方法

        Scanner sc = new Scanner(System.in);
        System.out.print("请出拳:1.剪刀2.石头3.布(输入相应数字):");
        perFist = sc.nextInt();
        switch (perFist) {
        case 1:
            System.out.println("你出拳:剪刀");
            break;
        case 2:
            System.out.println("你出拳:石头");
            break;
        case 3:
            System.out.println("你出拳:布");
            break;
        }
    }
}

机器人类

package com.guess;

import java.util.Scanner;

/**
 * 机器出拳
 * 
 * @author hp
 *
 */
public class Computer {
    String machineName;// 机器人物姓名
    int perScore;// 机器人得分
    int compFist;// 选择机器人出拳编号

    /**
     * 选择对方角色
     */
    public void selectMachine() {

        Scanner sc = new Scanner(System.in);
        System.out.print("请选择对方角色(1.刘备\t2.孙权\t3.曹操):");
        int compNum = sc.nextInt();
        if (compNum == 1) {
            machineName = "刘备";
        } else if (compNum == 2) {
            machineName = "孙权";
        } else {
            machineName = "曹操";
        }
    }

    /**
     * 机器人出拳
     */
    public void computerFist() {
        compFist = (int) (Math.random() * 10) % 3 + 1;// 产生随机数
        switch (compFist) {
        case 1:
            System.out.println(machineName + "出拳:剪刀");
            break;
        case 2:
            System.out.println(machineName + "出拳:石头");
            break;
        case 3:
            System.out.println(machineName + "出拳:布");
            break;
        }
    }
}

游戏类

package com.guess;

import java.util.Scanner;

/**
 * 游戏类
 * 
 * @author hp
 *
 */
public class Game {
    int count;// 对战次数
    Person pg = new Person(); // 甲方
    Computer cg = new Computer();// 乙方

    /**
     * 开始比赛
     */
    public void start() {
        System.out.println("\t\t\t********************");
        System.out.println("\t\t\t*****游戏             开始*****");
        System.out.println("\t\t\t********************");
        System.out.println("出掌规则:1.剪刀\t2.石头\t3.布");
        String flag;
        boolean b = false;
        Scanner sc = new Scanner(System.in);
        cg.selectMachine(); // 选择对方角色
        System.out.print("请输入你的姓名:");
        pg.name = sc.next();
        System.out.println(pg.name + " VS " + cg.machineName + "\n\n");
        do {

            if (!b) {
                System.out.print("要开始吗?(y/n)");
                b = true;
            } else {
                System.out.print("是否开始下一轮?(y/n)");
            }
            flag = sc.next();
            if ("y".equals(flag)) {
                pg.showFist();
                cg.computerFist();
                select();
            } else {
                System.out.println("----------------------------------");
                initial();
            }
        } while (!"n".equals(flag));
    }

    /**
     * 比赛判断
     */

    public void select() {
        count++;
        if (((pg.perFist == 1) && (cg.compFist == 3))
                || ((pg.perFist == 2) && (cg.compFist == 1))
                || ((pg.perFist == 3) && (cg.compFist == 2))) {
            pg.perScore++;
            System.out.println("结果:你赢了,真棒!");
        } else {
            cg.perScore++;
            System.out.println("结果:你输了,真笨!");
        }
    }

    /**
     * 结局判断
     */
    public void initial() {
        System.out.println(pg.name + " VS " + cg.machineName);
        System.out.println("对战次数:" + count + "\n");
        System.out.println("姓名\t" + "得分");
        System.out.println(pg.name + "\t" + pg.perScore);
        System.out.println(cg.machineName + "\t" + cg.perScore + "\n");
        if (pg.perScore > cg.perScore) {
            System.out.println("结果:恭喜,你赢了!");
        } else {
            System.out.println("结果:呵呵,你输了!");
        }
    }

}

主类,游戏开始类

package com.guess;

/**
 * 测试主类
 * 
 * @author hp
 *
 */
public class StartGame {
    public static void main(String[] args) {
        Game g = new Game(); // 实例化game类
        g.start(); // 调用game类方法开始游戏
    }
}

程序运行截图
这里写图片描述
面向对象的思想,一个类中,当有这个类存在这个属性,这个属性可以是成员变量,否则只能是局部变量;方法也是如此

posted @ 2017-07-25 12:28  _小龙人  阅读(166)  评论(0编辑  收藏  举报