* Computer 电脑类
* 属性 :角色名,得分。
* 方法:出拳的方法
1 /** 2 * Computer 电脑类 3 * 属性 :角色名,得分。 4 * 方法:出拳的方法 5 * @author lisi 6 * @version 版本 1.0 7 * 8 */ 9 public class Computer { 10 // 属性 昵称,得分 11 String computer_name = "电脑";// null 12 int computer_score = 0;// 0 13 14 //出拳方法 int --- 参数无参数。实例方法,对象方法 不被static修饰。 15 /** 16 * Computer 出拳方法 17 * @return 电脑出的拳:1 剪刀 2 石头 3 布 18 */ 19 public int computerShowFist() { 20 int show = (int)(Math.random()*3)+1; 21 switch (show) { 22 case 1: 23 System.out.println(computer_name + "出了剪刀"); 24 25 break; 26 case 2: 27 System.out.println(computer_name + "出了石头"); 28 break; 29 case 3: 30 System.out.println(computer_name + "出了布"); 31 break; 32 33 34 } 35 return show; 36 37 } 38 39 40 41 }
1 public class ComputerTest { 2 public static void main(String[] args) { 3 Computer computer = new Computer(); 4 int show = computer.computerShowFist(); 5 System.out.println(show); 6 7 } 8 9 }
1 import java.util.Scanner; 2 3 /** 4 * Person 类 5 * 名字 6 * 分数 7 * 出去的功能 8 * @author lisi 9 * 10 */ 11 public class Person { 12 String person_name = "匿名"; 13 int person_score = 0; 14 15 16 // 人出拳 17 /** 18 * 人出拳的方法 19 * @return 人出的拳头;1 剪刀 2 石头 3 布 20 */ 21 public int personShowFist() { 22 Scanner sc = new Scanner(System.in); 23 System.out.println("请出拳:1 剪刀 2 石头 3 布 "); 24 int show = sc.nextInt(); 25 switch (show) { 26 case 1: 27 System.out.println(person_name + "出了剪刀"); 28 29 break; 30 case 2: 31 System.out.println(person_name + "出了石头"); 32 break; 33 case 3: 34 System.out.println(person_name + "出了布"); 35 break; 36 37 38 } 39 40 return show; 41 } 42 43 }
1 import java.util.Scanner; 2 3 /** 4 * 游戏类 5 * 操作 管理 上面定义的Person类和Computer类, 6 * 调用各自出拳的方法,去做比较,输赢。 7 * @author 8 * 9 */ 10 public class Game { 11 12 // 把 Person 和 Computer,作为自己的属性值。 13 Person person; 14 Computer computer;// 类作为另外一个类的成员。 15 // 引用数据类型默认值:--- null- --- 空指针 16 // 定义属性count表示对战的次数 17 int count = 0; 18 19 20 public void inital() { 21 if(person == null) { 22 person = new Person(); 23 } 24 if(computer == null) { 25 computer = new Computer(); 26 } 27 } 28 29 30 // 开始游戏的方法: 31 public void startGame() { 32 Scanner sc = new Scanner(System.in); 33 System.out.println("-----------欢迎加入猜拳游戏--------------"); 34 System.out.println(); 35 System.out.println("************************************"); 36 System.out.println("出拳的规则 : 1 剪刀 2 石头 3 布"); 37 System.out.println("**************猜拳开始********************"); 38 System.out.println("****************************************"); 39 System.out.println(); 40 41 // 调用初始化方法 42 inital(); 43 // 加入角色名 44 System.out.println("选择角色:1 曹操 2 吕布 3 孙权"); 45 int role = sc.nextInt(); 46 47 switch (role) { 48 case 1: 49 computer.computer_name = "曹操"; 50 break; 51 case 2: 52 computer.computer_name = "吕布"; 53 break; 54 case 3: 55 computer.computer_name = "孙权"; 56 break; 57 } 58 System.out.println("请输入你的名字:"); 59 person.person_name = sc.next(); 60 61 System.out.println(person.person_name + " PK " + computer.computer_name +" 对战"); 62 System.out.println("是否开始游戏:y / n"); 63 String answer = sc.next(); 64 65 while("y".equals(answer)) { 66 67 68 // 调用各种的出拳: 69 int personFist = p