剪刀,石头,布游戏——类的初学习。

package com.lovo;


/**
 * 计算机玩家
 * @author 刘顺
 *
 */
public class Computer {
	private String name;
	private int fist;

	/**
	 * 构造器
	 * name 计算机玩家的名字
	 */
	public Computer(String name) {
		this.name = name;
	}

	/**
	 * 出拳方法
	 */
	public void showFist() {
		this.fist = (int) (Math.random() * 3 + 1);
	}

	/**
	 * 获得玩家名字
	 * 返回玩家名字
	 */
	public String getName() {
		return name;
	}

	/**
	 * 获得玩家出的拳
	 * 返回玩家出拳对应的数字
	 */
	public int getFist() {
		return fist;
	}

	/**
	 * 显示出拳信息
	 * 返回出拳信息的字符串
	 */
	public String showInfo() {
		String str = name + "出的是";
		switch (fist) {
		case 1:
			str += "剪刀";
			break;
		case 2:
			str += "石头";
			break;
		case 3:
			str += "布";
			break;
		}
		return str;
	}
}
/**
*上一段代码是写了电脑类玩家,
*下一段代码写了一个人类玩家。
*/
package com.lovo;

/**
 * 人类玩家
 * @author 刘顺
 *
 */
public class Human {
	private String name;
	private int fist;
	
	/**
	 * 构造器
	 * name 玩家的姓名
	 */
	public Human(String name) {
		this.name = name;
	}

	/**
	 * 出拳
	 * fist 出拳对应的数字
	 */
	public void showFist(int fist) {
		this.fist = fist;
	}

	/**
	 * 获得玩家姓名
	 * 返回玩家的姓名
	 */
	public String getName() {
		return name;
	}

	/**
	 * 获得玩家出的拳
	 * 返回玩家出拳对应的数字
	 */
	public int getFist() {
		return fist;
	}

	/**
	 * 显示出拳信息
	 * 返回出拳信息的字符串
	 */
	public String showInfo() {
		String str = name + "出的是";
		switch (fist) {
		case 1:
			str += "剪刀";
			break;
		case 2:
			str += "石头";
			break;
		case 3:
			str += "布";
			break;
		}
		return str;
	}
}

package com.lovo;
//**
*以下是一段测试代码的主程序。
*
*/

import java.util.Scanner;

public class Test01 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Computer c = new Computer("奥特曼");
		Human h = new Human("张飞");
		
		char goon;
		do {
			int choice;
			do {
				System.out.println("1. 剪刀;2. 石头;3. 布");
				System.out.print("请选择: ");
				choice = sc.nextInt();
			}
			while (choice < 1 || choice > 3);
			h.showFist(choice);
			System.out.println(h.showInfo());
			c.showFist();
			System.out.println(c.showInfo());
			switch (h.getFist()) {
			case 1:
				switch (c.getFist()) {
				case 1:
					System.out.println("平局!!!");
					break;
				case 2:
					System.out.println(c.getName() + "胜!!!");
					break;
				case 3:
					System.out.println(h.getName() + "胜!!!");
				}
				break;
			case 2:
				switch(c.getFist()) {
				case 1:
					System.out.println(h.getName() + "胜!!!");
					break;
				case 2:
					System.out.println("平局!!!");
					break;
				case 3:
					System.out.println(c.getName() + "胜!!!");
					break;
				}
				break;
			case 3:
				switch(c.getFist()) {
				case 1:
					System.out.println(c.getName() + "胜!!!");
					break;
				case 2:
					System.out.println(h.getName() + "胜!!!");
					break;
				case 3:
					
					System.out.println("平局!!!");
					break;
				}
				break;
			}
			System.out.print("继续吗?(y|n)");
			goon = sc.next().toLowerCase().charAt(0);
		} while(goon == 'y');
		
		System.out.println("游戏结束!!!");
		sc.close();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值