人机猜拳


computer.java

public class Computer {
	String name;
	int score;

	public int showFirst() {
		int num = (int) Math.random() * 3 + 1;
		switch (num) {
		case 1:
			System.out.println("电脑出拳:剪刀");
			break;
		case 2:
			System.out.println("电脑出拳:石头");
			break;
		case 3:
			System.out.println("电脑出拳:布");
			break;
		default:
			break;
		}
		return num;
	}

}
Person.java

import java.util.Scanner;

public class Person {
	String name;
	int score;
	Scanner sc=new Scanner(System.in);
	public int showFirst() {
		System.out.println("请输入你的结果:1、剪刀  2、石头  3、布");
		int num=sc.nextInt();
		switch (num) {
		case 1:
			System.out.println("人的结果:剪刀");
			break;
		case 2:
			System.out.println("人的结果:石头");
			break;
		case 3:
			System.out.println("人的结果:布");
			break;
		default:
			break;
		}
		return num;
	}
}
Game.java

import java.util.Scanner;

public class Game {
	Person p = new Person();
	Computer c = new Computer();
	//Game g = new Game();
	int count = 0;
	int win = 0;
	String isGo;

	public void start() {
		System.out.println("----------欢迎来到人机猜拳----------");
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的名字:");
		p.name = sc.nextLine();
		System.out.println("请选择你的对手:1、刘备    2、吕布   3、曹操");
		int in = sc.nextInt();
		switch (in) {
		case 1:
			System.out.println(p.name + "VS刘备");
			break;
		case 2:
			System.out.println(p.name + "VS吕布");
			break;
		case 3:
			System.out.println(p.name + "VS曹操");
			break;

		default:
			System.out.println();
			break;
		}
		do {
			int rePerson = p.showFirst();
			int reCompu = c.showFirst();
			if ((rePerson == 1 && reCompu == 3)
					|| (rePerson == 2 && reCompu == 1)
					|| (rePerson == 3 && reCompu == 2)) {
				System.out.println("恭喜你赢了");
				p.score++;
				count++;
				win++;
			} else if (rePerson == reCompu) {
				System.out.println("平局");
				count++;
			} else {
				System.out.println("你输了");
				p.score--;
				count++;
			}
			System.out.println("是否继续y/n");
			isGo = sc.next();
			show();
		}

		while (!isGo.equals("n"));
		
	}

	public void show() {
		System.out.println("一共对战了" + count + "局");
		System.out.println("你胜利了" + win + "次");
	}

}
Test.java

public class TestGuess {

	public static void main(String[] args) {
		Game g=new Game();
		g.start();
				
	}
}






















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值