通过构建玩家类,计算机类,游戏类,三个类,实现了简单的人机猜拳小游戏。

/**
* @author  万星明
* @version 创建时间:2018年10月20日 下午3:01:09
* 计算机类
*/
public class Computer {public static String name = "计算机";public static int score = 0;//出拳方法public static int finger() {int data = (int)(Math.random()*3+1);switch(data) {case 1:System.out.println(name+"出拳:剪刀");break;case 2:System.out.println(name+"出拳:石头");break;case 3:System.out.println(name+"出拳:布");break;}return data;}
}
import java.util.Scanner;
/**
* @author  万星明
* @version 创建时间:2018年10月20日 下午2:46:49
* 用户类
*/
public class User {static Scanner sc = new Scanner(System.in);public static String name = "万星明";public static int score=0;//出拳方法public static int finger() {System.out.println("请出拳:1.剪刀 2.石头 3.布(输入相应数字):");int data = sc.nextInt();switch(data) {case 1:System.out.println("你出拳:剪刀");break;case 2:System.out.println("你出拳:石头");break;case 3:System.out.println("你出拳:布");break;default:System.out.println("输入错误!");break;}return data;}
}
import java.util.Scanner;
/**
* @author  万星明
* @version 创建时间:2018年10月20日 下午3:06:39
* 游戏类
*/
public class Game {static Scanner sc = new Scanner(System.in);User user;Computer computer;static int fightFrequency=0;//初始化方法public static void initial() {System.out.println("----------------欢迎进入游戏世界----------------");System.out.println("\n\t\t\t\t************");System.out.print("\t\t\t\t**猜拳**开始**");System.out.print("\n\t\t\t\t************");System.out.println("\n出拳规则:1.剪刀 2.石头 3.布");}//开始游戏方法public static void startGame() {System.out.printf("请选择计算机角色:1.刘备 2.曹操 3.孙权:");int answer = sc.nextInt();System.out.println("请输入你的姓名:");User.name = sc.next();switch(answer) {case 1:Computer.name = "刘备";System.out.println(User.name+" VS "+Computer.name);break;case 2:Computer.name = "曹操";System.out.println(User.name+" VS "+Computer.name);break;case 3:Computer.name = "孙权";System.out.println(User.name+" VS "+Computer.name);break;default:System.out.println("无此人物选项!");break;}}//第一次游戏方法public static void showFirst() {initial();startGame();int userNum = User.finger();int computerNum = Computer.finger();calResult(userNum, computerNum);while(true) {System.out.println("是否开始下一轮?(y/n):");String answer = sc.next();if(answer.equalsIgnoreCase("y")) {int userNum1 = User.finger();int computerNum1 = Computer.finger();calResult(userNum1, computerNum1);}else {break;}}}//计算结果方法public static void calResult(int userNum,int computerNum) {if((userNum==1&&computerNum==3)||(userNum==2&&computerNum==1)||(userNum==3&&computerNum==2)) {User.score++;System.out.println("结果说:^_^你赢了,帅哥!");}else if((userNum==1&&computerNum==2)||(userNum==2&&computerNum==3)||(userNum==3&&computerNum==1)){Computer.score++;System.out.println("结果说:^_^你输了,笨蛋!");}else {System.out.println("结果说:^_^竟然是平局!!");}fightFrequency++;}//显示最终游戏结果方法public static void showResult() {System.out.println(Computer.name+":"+Computer.score+" VS "+User.score+":"+User.name);System.out.println("对战次数:"+fightFrequency);if(Computer.score>User.score) {System.out.println("笨笨,加油啊!虽然你输了,但是!你萌啊!");}else if(Computer.score<User.score) {System.out.println("小伙子,厉害哦!电脑都赢不了你了!");}else {System.out.println("你也就和电脑一般智商,一般般!");}}//主运行方法public static void main(String[] args) {showFirst();showResult();}
}

java实现人机猜拳小游戏相关推荐

  1. 人机猜拳小游戏(类和对象)

    首先,创建四个java类分别是用户类User,电脑类Computer,游戏类Game以及测试类Test; 代码如下: 1.用户类 package 类和对象; import java.util.Scan ...

  2. 人机猜拳代码python_python实现人机猜拳小游戏

    今天的这篇文章呢是对人机猜拳小游戏--石头剪刀布的一个描述以及代码展现 石头剪刀布游戏代码的简介:关于石头剪刀布这个小游戏,大致得到思路就是,玩家出一个手势,然后电脑再随机出一个手势,最后再判断是玩家 ...

  3. python人机猜拳_python实现人机猜拳小游戏

    今天的这篇文章呢是对人机猜拳小游戏--石头剪刀布的一个描述以及代码展现 石头剪刀布游戏代码的简介:关于石头剪刀布这个小游戏,大致得到思路就是,玩家出一个手势,然后电脑再随机出一个手势,最后再判断是玩家 ...

  4. python编程猜拳小游戏_python实现人机猜拳小游戏

    今天的这篇文章呢是对人机猜拳小游戏--石头剪刀布的一个描述以及代码展现 石头剪刀布游戏代码的简介:关于石头剪刀布这个小游戏,大致得到思路就是,玩家出一个手势,然后电脑再随机出一个手势,最后再判断是玩家 ...

  5. Java代码编写猜拳小游戏

    Java代码编写猜拳小游戏 import java.util.Random; import java.util.Scanner;public class Guess {public static vo ...

  6. Java人机猜拳小游戏

    完成人机猜拳互动游戏的开发 阶段一:实验--分析业务,创建用户类 1.分析业务,抽象出类.类的特征和行为 2.创建出用户类 阶段二:实验--创建出计算机类 创建计算机类Computer.实现计算机出拳 ...

  7. java博弈,人机博弈小游戏(Java)

    人机博弈小游戏 实现功能 电脑随机出拳 玩家任意出拳 五局三胜制 可判断最终赢家 下面展示 代码. // A code block var foo = 'bar'; // An highlighted ...

  8. 实现石头剪刀布获胜法_用java实现一个猜拳小游戏

    本文实例为大家分享了java实现猜拳小游戏的具体代码,供大家参考,具体内容如下 项目名称 猜拳小游戏 项目描述 玩家与电脑进行猜拳游戏,玩家行为采用输入方式,电脑行为采用随机形式. 实现: User类 ...

  9. 【java学习】猜拳小游戏

    猜拳小游戏,实现内容:选择对战人物,记录对战详情,循环对战,记录胜场,第21行的ROBOT方法思路不错,记录一下 package day1204;import day1203.Acount;impor ...

最新文章

  1. ERP与GMP结合在药类企业实施及应用
  2. 锤子科技犯过的构图错误你一定也犯过
  3. cuDNN version incompatibility: PyTorch was compiled against 7005 but linked against 6021 问题解决...
  4. [Django青铜修炼手册] 初识Django
  5. spring中的IOC和AOP
  6. Matlab矩阵、元胞数组的合并拼接
  7. 硬盘引导扇区、多分区图、不通硬盘的LINUX逻辑分区数量
  8. 理解分布式和区块链技术
  9. Vue启动项目报错travel@1.0.0 dev: `webpack-dev-server--inline --progress --config build/webpack.dev.conf.js
  10. php 微信小程序签到功能,微信小程序每日签到
  11. python进行谱曲_人工智能可以作曲吗?
  12. 硬件产品的成本构成——研发、产品边际成本和服务
  13. 机器学习实践—基于Scikit-Learn、Keras和TensorFlow2第二版—第2章 端到端机器学习项目实践
  14. 西门子plc200 c语言转换,西门子S7-200 系列PLC量程转换及编程方法
  15. C#中包含英文月份的美式日期输出格式
  16. 如何添加装饰螺纹线规格
  17. 2021年江苏省淮安高考成绩查询,2021年江苏淮安高考时间:6月7日至9日
  18. MATLAB实现(7,4)汉明码的编码解码纠错及BER的分析
  19. 隐藏IDEA的行首的黄色小灯泡
  20. matlab下载实录:matlab2022a好用吗

热门文章

  1. springboot整合单元测试
  2. python str bytes转换
  3. PCF8591学习笔记
  4. 乐优购物学习笔记(15)
  5. Android Webview 漏洞复现
  6. python的编程工具spider_python-01 spider原理
  7. Python安装pymysql
  8. python的scatter函数_python scatter函数用法实例详解
  9. 电子类公司笔试题精选(zhuan)
  10. Eventbus-Rxjava购物车