石头,剪刀,布游戏
2014-07-17 08:35
405 查看
import java.util.Scanner; import java.util.InputMismatchException; * 游戏:石头,剪刀,布 * 编写可以玩最流行的剪刀=石头=布游戏程序 * 程序提示电脑随机产生一个数,这个数为0,1或者2,分别表示石头,剪刀和布。 * 程序提示用户输入0,1或者2,然后显示一个消息,表明用户 和计算机谁赢了游戏,谁输了游戏,或者打成平手。 * 同时直到用户或者计算机连续赢两次以上,程序终止 */ public class Game { int per = 0;//全局变量用来存放电脑和人赢得次数! int com = 0; public int result(int personInput, int computerInput) { if (personInput == computerInput) { if (personInput == 0) { System.out.println("我出石头,电脑也出石头!"); System.out.println("我们是平局!!!!"); per = 0; com = 0; } else if (personInput == 1) { System.out.println("我出剪刀,电脑也出剪刀!"); System.out.println("我们是平局!!!!"); per = 0; com = 0; } else if (personInput == 2) { System.out.println("我出布,电脑也出布!"); System.out.println("我们是平局!!!!"); per = 0; com = 0; } } else if ((personInput == 0 && computerInput == 1) || (personInput == 1 && computerInput == 2) || (personInput == 2 && computerInput == 0)) { if (personInput == 0) { System.out.println("我出石头!!"); System.out.println("电脑出剪刀"); System.out.println("我赢了 !!!"); per += 1; com = 0; } else if (personInput == 1) { System.out.println("我出剪刀!!"); System.out.println("电脑出布"); System.out.println("我赢了 !!!"); per += 1; com = 0; } else if (personInput == 2) { System.out.println("我出布!!"); System.out.println("电脑石头!"); System.out.println("我赢了 !!!"); per += 1; com = 0; } } else if ((personInput == 0 && computerInput == 2) || (personInput == 1 && computerInput == 0) || (personInput == 2 && computerInput == 1)) { if (personInput == 0) { System.out.println("我出石头!!"); System.out.println("电脑布"); System.out.println("电脑赢了 !!!"); com += 1; per = 0; } else if (personInput == 1) { System.out.println("我出剪刀!!"); System.out.println("电脑出石头!!"); System.out.println("电脑赢了 !!!"); com += 1; per = 0; } else if (personInput == 2) { System.out.println("我出布!!"); System.out.println("电脑出剪刀!!!"); System.out.println("电脑赢了 !!!"); com += 1; per = 0; } } if (per >= 2) { System.out.println("我赢了" + per + "局!!"); return per; } else if (com >= 2) { System.out.println("电脑赢了" + com + "局!!"); return com; } else return 0; } public static class Person { public int input() { System.out.println("请输入:0(0,代表石头),1(1.代表剪刀),2(2代表布):"); Scanner sc = new Scanner(System.in); int personInput = -1; boolean Isend = false; while (!Isend) { try { personInput = sc.nextInt(); Isend = true; } catch (InputMismatchException e) { System.out.println("输入错误!请重新输入0(0,代表石头),1(1.代表剪刀),2(2代表布)!!!"); sc.nextLine(); Isend = false; } } return personInput; } } public static class Computer //电脑随即生成的随机数 { public int random() { int h = (int) (Math.random() * 3);//生成0--3之间的随机数(包含0) return h; } } public static void main(String[] args) { Person person = new Person(); Computer computer = new Computer(); Game game = new Game(); boolean end = false; while (!end) { int a = game.result(person.input(), computer.random()); if (a >=2) { end = true; } } } }
相关文章推荐
- 自己写的一个小的剪刀——石头——布游戏的GUI程序
- 剪刀,石头,布游戏——类的初学习。
- 一个C语言的剪刀石头布小游戏
- 以写代学:python实现 剪刀、石头、布游戏
- 用C#语言在Unity中实现剪刀石头布的游戏
- c++石头-剪刀-布游戏c++程序设计原理与实践第4章习题
- java基础总结 --- enum枚举实现多路分发 石头、剪刀、布游戏示例
- 关于类似“石头、剪刀、布”游戏,需要循环判断的,判断条件设计技巧
- 石头、剪刀布游戏
- 命名管道实现进程间通信--石头、剪刀、布游戏 分类: linux 2014-06-01 22:50 467人阅读 评论(0) 收藏
- 编写剪刀(scissor)、石头(stone)、布(cloth)游戏
- HTML5游戏开发,剪刀石头布小游戏案例
- 石头-剪刀-布游戏
- 命名管道实现进程间通信--石头、剪刀、布游戏
- Java语言程序设计-基础篇-4.34习题--游戏:石头剪刀,布
- c#游戏 剪刀石头
- 第3章:游戏:石头,剪刀,布
- 石头,剪刀,布的游戏
- 石头-剪刀-布, 这个经典游戏的基本逻辑是这样的吗????
- 使用JavaScript写的剪刀石头布的游戏!