java 人机猜拳 游戏
2022/2/28 14:51:20
本文主要是介绍java 人机猜拳 游戏,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
人机猜拳-游戏
掌握类和对象的使用,掌握方法的定义和返回值,掌握封装的运用
- 定义一个电脑类:Computer.java
点击查看【Computer.java】代码
/** * @Title: 电脑类 * @Description: 定义电脑类创建时将拥有的属性和方法 * @author: TabKey9 * @CreateDate: 2022.2.26 */ public class Computer { static String name = "关羽";//名字 static double integral;//总积分 static String chu;//本次出拳状态 /** * @description: 出拳动作 * @param: null * @return: String value * @author: TabKey9 * @update: 2022/2/26 17:35 */ public static void chu(){ int val = (int)(Math.random()*3) + 1;//1个随机数:取值范围0~2 switch(val){ case 1: chu = "石头"; System.out.println( name +" 电脑出【石头】"); break; case 2: chu = "剪刀"; System.out.println( name +" 电脑出【剪刀】"); break; case 3: chu = "布"; System.out.println(name +" 电脑出【布】"); break; } } }
2.定义一个玩家类:Mankind.java
点击查看【Mankind.java】代码
/** * @Title: 人类 * @Description: 定义人类创建时将拥有的属性和方法 * @author: TabKey9 * @CreateDate: 2022.2.26 */ public class Mankind { static String name = "王富贵";//名字 static double integral;//总积分 static String chu;//本次出拳状态 /** * @description: 出拳动作 * @param: null * @return: String value * @author: TabKey9 * @update: 2022/2/26 17:35 */ public static void chu(int i){ switch(i){ case 1: chu = "石头"; System.out.println( name +" 出【石头】"); break; case 2: chu = "剪刀"; System.out.println( name +" 出【剪刀】"); break; case 3: chu = "布"; System.out.println(name +" 出【布】"); break; } } }
- 最后再写一个游戏类:Game.java
点击查看【Game.java】代码
import java.util.Scanner; /** * @Title: 人机猜拳-游戏 * @Description: 掌握类和对象的使用,掌握方法的定义和返回值,掌握封装的运用 * @author: TabKey9 * @CreateDate: 2022.2.26 */ public class Game { public static void main(String[] args) { System.out.println("【========== 人机猜拳游戏 ==========】"); Scanner input = new Scanner(System.in);// 创建扫描仪,用于获取用户输入值 Computer dn = new Computer();//创建一个电脑玩家 System.out.println("请选择电脑玩家:1、小伙子;2、小姑凉;2、关羽;4、唐僧"); switch(input.nextInt()){ case 1: dn.name ="小伙子"; break; case 2: dn.name ="小姑凉"; break; case 3: dn.name ="关羽"; break; case 4: dn.name ="唐僧"; break; } Mankind ren = new Mankind();//创建一个玩家 System.out.print("请输入玩家昵称:"); ren.name = input.next(); while(true){ System.out.print("【三局两胜】开始游戏?(y/n):");// 开始游戏? String start = input.next(); if (start.equals("n")){ // 判断字符串需要用字符串的函数方法,而不是用运算符比较字符串 System.out.println("游戏结束,下次再玩吧!"); System.exit(0);// 终止游戏 }else if (!start.equals("y")){ System.out.println("请按要求重新输入!!!"); continue; } System.out.println("游戏规则:1、石头;2、剪刀;3、布");// 声明游戏规则 for (int i=1,d=0,r=0;i<3 || d==r;i++){ System.out.println(); System.out.print("请出拳:"); int val = input.nextInt(); ren.chu(val);//玩家出拳一次 dn.chu();//电脑出拳一次 // 判断胜负结果 if ( (dn.chu=="剪刀" && ren.chu=="石头") || (dn.chu=="石头" && ren.chu=="布") || (dn.chu=="布" && ren.chu=="剪刀") ){ r++; System.out.println("人/机 比分:"+ d +":"+ r); System.out.println( "你 得分!"); }else if ( dn.chu == ren.chu ){ i--;// 平局还得i--,不然1:0也会提前结束游戏 System.out.println("人/机 比分:"+ d +":"+ r); System.out.println("平局!再来?"); }else{ d++; System.out.println("人/机 比分:"+ d +":"+ r); System.out.println( "电脑 得分"); } // 累计总积分:输了--,赢了++ if ( r==2 && d<r ){ ren.integral++; dn.integral--; }else if ( d==2 && d>r ){ ren.integral--; dn.integral++; } } // 展示总积分 System.out.println(); System.out.println("你 的总积分:"+ ren.integral +";\t\t"+ dn.name +" 的总积分:"+ dn.integral); System.out.println(); } } }
游戏体验:
这篇关于java 人机猜拳 游戏的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)