石头,剪刀,布游戏
来源:互联网 发布:批发商也开淘宝不 编辑:程序博客网 时间:2024/04/28 14:14
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; } } }}
0 0
- 剪刀、石头、布游戏
- 剪刀石头布游戏
- 剪刀石头布游戏
- 石头剪刀布游戏
- 剪刀石头布游戏
- 石头剪刀布游戏
- 石头,剪刀,布游戏
- 石头-剪刀-布游戏
- 石头剪刀布游戏
- 石头剪刀布游戏
- 石头、剪刀布游戏
- 游戏_石头,剪刀,布
- python 石头剪刀布游戏
- 石头、剪刀、布猜拳游戏
- 8.5 剪刀石头布游戏
- ***4.34-游戏:剪刀石头布
- 剪刀石头布《静态游戏》
- [AHK]石头剪刀布游戏
- K&R The C Programming language 2
- 字符指针与字符数组
- 循环结构
- 先有鸡还是先有蛋?先有操作系统,还是先有汇编器?(对计算机本质的探讨)
- 自主性学习,用实践开路
- 石头,剪刀,布游戏
- 读u-boot-2010.06代码(一)
- phpMyAdmin为用户修改密码后拒绝访问
- 2013通化邀请赛部分题解
- 读U-boot-2010.06代码(二)
- hdu 4007 Dave 求矩形圈点最大值
- 数组NSArray
- 重载和重写的区别
- 笔试面试基础