java 小游戏人机猜拳 关键代码
2016-09-14 15:47
525 查看
<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>
相关文章推荐
- Spring之AOP(二)
- Java编写水仙花数
- Java判断101-200之间有多少个素数,并输出所有素数。
- wamp和Eclipse搭建PHP环境
- java中创建线程主要的三种方式
- java工厂模式
- MAC 下卸载 JAVA
- scala eclipse plugin 插件安装
- java并发编程:创建线程和进程
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- Commons- BeanUtils学习笔记
- android eclipse错误集合
- Java jdbc批量多线程读取CVS文件入库性能优化篇
- 【SpringMVC学习07】SpringMVC中的统一异常处理
- struts:poi读取excel文件(兼容2003、2007)
- java和js交互1
- Java选择排序
- hibernat异常:Unable to get the default Bean Validation factory
- java源码学习之List集合(一)
- eclipse Git & maven 安装