java 小游戏人机猜拳 关键代码

本文提供了一个简单的Java实现的人机猜拳小游戏关键代码,包括玩家类(person)、电脑对手类(comper)以及游戏逻辑类(game)。通过Scanner获取用户输入,Math.random()实现电脑随机出拳,通过switch-case判断胜负并更新得分,直至游戏结束。

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

<span style="font-size:18px;">import java.util.Scanner;

public class person {
	String name;
	int score;

	public int chuquan() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入:1.剪刀2.石头3.布");
		System.out.println("请出拳:");
		int choice = input.nextInt();
		switch (choice) {
		case 1:
			System.out.println("剪刀");
			break;
		case 2:
			System.out.println("石头"); ;
			break;
		case 3:
			System.out.println("布");
			break;
		}
		return choice;
	}

}
********************
public class comper {
	String name;
	int score;

	public  int chuquan() {
		int choice = (int) (Math.random() * 3) + 1;
		switch (choice) {
		case 1:
			System.out.println("剪刀");
			break;
		case 2:
			System.out.println("石头"); ;
			break;
		case 3:
			System.out.println("布");
			break;
		}
		return choice;
	}}
**********************
import java.util.Scanner;

public class game {
	person jia;
	comper yi;
	int count;

	public void initial() {
		jia = new person();
		yi = new comper();
		count = 0;
	}

	public void qidong() {
		initial();
		System.out.println("请选择对方英雄:(1:刘备2:孙权3:曹操)");
		Scanner input = new Scanner(System.in);
		System.out.println("请选择对方人物:");
		int num = input.nextInt();
		switch (num) {
		case 1:
			yi.name="刘备";
			break;
		case 2:
			yi.name="孙权";
			break;
		case 3:
			yi.name="曹操";
			break;
		}
		System.out.println("你选择了" + yi.name + "对战!");
		System.out.println("要开始吗?(y/n)");
		String go = input.next();
		System.out.println("请输入你的名字:");
		jia.name=input.next();
		while ("y".equals(go)) {
			int persons = jia.chuquan();
			int compers = yi.chuquan();
			if ((persons == 1 && compers == 1)
					|| (persons == 2 && compers == 2)
					|| (persons == 3 && compers == 3)) {
				System.out.println("和局!衰!");
			} else if ((persons == 1 && compers == 3)
					|| (persons == 2 && compers == 1)
					|| (persons == 3 && compers == 2)) {
				System.out.println("恭喜你 你赢了!");
				jia.score++;
			} else {
				System.out.println("你输了,你真衰!");
				yi.score++;
			}
			count++;
			System.out.println("是否开始?(y/n)");
			go=input.next();
		}
		System.out.println("-----------------------------------------");
		System.out.println(jia.name+"VS"+yi.name);
		System.out.println("对战次数:"+count);
		System.out.println("\n姓名:\t得分:");
		System.out.println(jia.name+"\t"+jia.score);
		System.out.println(yi.name+"\t"+yi.score);
		if(jia.score>yi.score){
			System.out.println("你最棒!");
		}else if(jia.score<yi.score){
			System.out.println("你真笨!");
		}else{
			System.out.println("棋逢对手!");
		}
		
	}
}

	
*****************
import java.util.Scanner;


public class testt {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		game game=new game();
		System.out.println("欢迎进入游戏世界");
		System.out.println("**************************");
		System.out.println("**猜拳,开始**");
		System.out.println("**************************");
		game.qidong();
	}

}</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值