第十二章 人机猜拳
2013-12-12 16:35
274 查看
package ch12; import java.util.Random; public class Computer { public String name; public int score; public int showFist() { Random r = new Random(); int num = r.nextInt(3) + 1; switch (num) { case 1: . System.out.println(name+"出的是剪刀"); break; case 2: System.out.println(name+"出的是石头"); break; case 3: System.out.println(name+"出的是布"); break; } return num; } } [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片 package ch12; import java.util.Scanner; public class Person { public String name; public int score; /** * 用户出拳 * * @return 返回用户出拳是什么 1 .剪刀 2.石头 3.布 */ public int showFirst() { Scanner input = new Scanner(System.in); int ch = 0; boolean b = true; while (b) { System.out.println("1.剪刀\t2.石头\t3.布"); ch = input.nextInt(); switch (ch) { case 1: System.out.println("您出的是剪刀"); b = false; break; case 2: System.out.println("您出的是石头"); b = false; break; case 3: System.out.println("您出的是布"); b = false; break; default: System.out.println("输入错误,请输入1--3 任意整数"); b = true; break; } } return ch; } } [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片 package ch12; import java.util.Scanner; public class Game { private Person person;// 人玩家 private Computer computer;// 电脑玩家 private int count; // 对战次数 Scanner input = new Scanner(System.in); private void init() { person = new Person(); computer = new Computer(); } public void satrtGame() { init(); int p = 0;// 代表用户出拳 int c = 0;// 代表电脑出拳 System.out.println("************************"); System.out.println("******* 猜拳 开始*******"); System.out.println("************************"); System.out.println("出拳规则1.剪刀2.石头3.布"); System.out.println("请选择对方角色(1.刘备2.孙权3.曹操)"); switch (input.nextInt()) { case 1: computer.name = "刘备"; break; case 2: computer.name = "孙权"; break; case 3: computer.name = "曹操"; break; } System.out.println("请输入您的姓名"); person.name = input.next(); System.out.println(person.name + "VS" + computer.name + "对战"); System.out.println("要开始吗y/n"); char a = input.next().charAt(0); if (a == 'y') { char b = 'y'; while (b == 'y') { System.out.print("请出拳"); p = person.showFirst(); c = computer.showFist(); if (p == c) { System.out.println("和局"); // 人赢 } else if (p == 1 && c == 3 || p == 2 && c == 1 || p == 3 && c == 2) { System.out.println("恭喜你赢了"); person.score += 1;// person.score=person.score+1; } else { System.out.println("你输了"); computer.score += 1; } count++; System.out.println("是否开始下一轮y/n"); b = input.next().charAt(0); } showResult();// 显示对战结果 } else { System.out.println("游戏退出"); } } // 显示对战结果 public void showResult(){ System.out.println(person.name+"VS"+computer.name); System.out.println("对战次数"+count); System.out.println(); System.out.println("姓名\t得分"); System.out.println(person.name+"\t"+person.score); System.out.println(computer.name+"\t"+computer.score); if(person.score>computer.score){ System.out.println("你真厉害赢了"); }else if(person.score<computer.score){ System.out.println("你真笨,输了"); }else{ System.out.println("平局"); } } } [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片 package ch12; public class TestGame { public static void main(String[] args) { Game game=new Game(); game.satrtGame(); } }
相关文章推荐
- java-第十二章-人机猜拳-分析业务,抽象出类,类的属性和方法,创建用户类
- java-第十二章-人机猜拳-创建计算机类
- 第十二章 人机猜拳小游戏
- Android程序——人机猜拳
- JAVA基础代码分享--模拟人机猜拳系统
- 2017-8-4java 人机猜拳(输赢判断) ,算法优化
- 第一本十三章人机猜拳
- 人机猜拳
- 书本上JAVA人机猜拳代码
- 02_人机猜拳
- 第十三章:人机猜拳
- java简单的人机猜拳小游戏
- 人机猜拳
- ACCPS1JAVA人机猜拳
- 基于控制台的人机猜拳小游戏
- JAVA语言实现人机猜拳小游戏
- 人机猜拳游戏(自己编写shell脚本)
- 人机猜拳
- 人机猜拳
- Java人机猜拳游戏(命令行版)