JAVA程序设计(07.2)-----面对对象设计练习 猜拳

本文介绍了一个简单的猜拳游戏实现过程,通过Java编程语言创建了猜拳游戏的类和对象,实现了电脑与玩家之间的互动猜拳,并能够判断输赢。文章详细展示了如何设置猜拳种类、转化猜拳为数字及判断输赢等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.妥妥的先建立对象

package com.lovo;

/**
 * 类:猜拳游戏~ 暂定3胜制吧~
 * @author Abe
 *
 */
/**
 * 构造器
 */

/**
 * 属性: 1. 石头剪刀布 2. 循环次数 3. 各自胜利次数
 */
public class Morra {
	private int finger = 0;
	private int times = 0;
	private int pcwin = 0;
	private int youwin = 0;

	/**
	 * 动作:电脑获取猜拳
	 */
	public void have() {
		finger = (int) (Math.random() * 3 + 1);
	}
	/**
	 * 动作:猜拳转化为数字 1剪刀 2石头 3布
	 */
	public int change(String x) {
		int yours = 3;
		if (x.equals("剪刀")) {
			yours = 1;
		} else if (x.equals("石头")) {
			yours = 2;
		}
		return yours;
	}
	/**
	 * 动作:比较输赢
	 */
	public String look(int yours) {
		if (finger == yours) {
			times++;
			return "平局~~ 你们玩了" + times + "局了~";
		} else if (finger == 1 && yours == 3 || finger == 2 && yours == 1
				|| finger == 3 && yours == 2) {
			pcwin++;
			times++;
			return "哎!这局电脑获胜了~~~你们玩了" + times + "局了~";
		}
		youwin++;
		times++;
		return "哦!这局你获胜了~~~你们玩了" + times + "局了~";
	}
	/**
	 * 调取胜利次数
	 */
	public int pcwin() {
		return pcwin;
	}
	public int youwin() {
		return youwin;
	}
}

然后是主程序

package com.lovo;
/**
 * 猜拳游戏开始~
 * @author Abe
 */
import java.util.Scanner;

public class Morratest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Morra pc = new Morra();

		do {
			System.out.print("请输入你要猜拳的种类:1石头 2剪刀 3布");
			int yours = sc.nextInt();
			pc.have();
			String hint = pc.look(yours);
			System.out.println(hint);

		} while (pc.pcwin() != 3 && pc.youwin() != 3);
		if (pc.pcwin() == 3) {
			System.out.println("电脑累计胜利3次~获得了最终胜利。");
		} else {
			System.out.println("你累计胜利3次~获得了最终胜利!!");
		}
		sc.close();
	}
}


老师的方法~更好的封装~ 有必要还可以加 输赢次数什么的 懒得做了 判断也没做 据说以后会讲 妥妥的~

先是类

package com.lovo;

/**
 * 类:猜拳游戏~
 * @author Abe
 *
 */

/**
 * 构造器
 */
public class Morra {
	public Morra(String str) {
		this.name = str;
	}

	/**
	 * 属性: 1. 石头剪刀布
	 */
	private String name = "";
	private int finger = 0;

	/**
	 * 动作:电脑获取猜拳
	 */
	public void setFinger() {
		this.finger = (int) (Math.random() * 3 + 1);
	}

	/**
	 * 动作:获取玩家输入的猜拳(1~3)
	 */
	public void setFinger(int x) {
		this.finger = x;
	}

	/**
	 * 动作:猜拳转化为数字 1剪刀 2石头 3布
	 */
	public String change(int x) {
		if (x == 1) {
			return "1剪刀";
		} else if (x == 2) {
			return "2石头";
		} else {
			return "3布  ";
		}
	}

	/**
	 * 调取名字,出的什么拳
	 */
	public String getName() {
		return name;
	}
	public int getFinger() {
		return finger;
	}
	
	/**
	 * 判断输赢 并返回字符串
	 * @param other
	 * @return
	 */
	public String open(Morra other) {
		switch (this.getFinger()) {
		case 1:
			switch (other.getFinger()) {
			case 1:
				return "平局";
			case 2:
				return other.getName() + "~ 赢了~~";
			default:
				return this.getName() + "~ 赢了~~";
			}
		case 2:
			switch (other.getFinger()) {
			case 1:
				return this.getName() + "~ 赢了~~";
			case 2:
				return "平局";
			default:
				return other.getName() + "~ 赢了~~";
			}
		default:
			switch (other.getFinger()) {
			case 1:
				return other.getName() + "~ 赢了~~";
			case 2:
				return this.getName() + "~ 赢了~~";
			default:
				return "平局";
			}
		}
	}
}

程序依旧那么简单~

package com.lovo;

/**
 * 猜拳游戏开始~
 * @author Abe
 */
import java.util.Scanner;

public class Morratest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Morra human = new Morra("小和尚");
		Morra pc = new Morra("法轮");


		System.out.print("请选择你要出的拳:1剪刀 2石头 3布");
		human.setFinger(sc.nextInt());
		System.out.println(human.getName() + "出了"
				+ human.change(human.getFinger()));
		pc.setFinger();
		System.out.println(pc.getName() + "出了" + pc.change(pc.getFinger()));
		System.out.println(human.open(pc));
		
		sc.close();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值