首页 > 其他分享 >猜拳小游戏案例面向对象

猜拳小游戏案例面向对象

时间:2023-02-24 10:55:39浏览次数:36  
标签:computer 猜拳 person System name 面向对象 小游戏 println out

* 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 = person.personShowFist();
 70             int computerFist = computer.computerShowFist();
 71             
 72             // 根据出的拳比较输赢 1 剪刀 2 石头 3 布
 73             
 74             if (personFist == 1 && computerFist == 3 || personFist == 2 && computerFist == 1||personFist == 3 && computerFist == 2) {
 75                 System.out.println(person.person_name + "赢");
 76                 // 添加人的分数
 77                 person.person_score += 1;
 78             }else if (personFist == computerFist) {
 79                 System.out.println("平局");
 80             }else {
 81                 System.out.println(computer.computer_name + "赢");
 82                 computer.computer_score += 1;
 83             }
 84             // 记录对战的次数:
 85             count ++;
 86             
 87             System.out.println("是否开始下一局:y / n");
 88             answer = sc.next();
 89         }
 90         
 91         
 92         // 循环结束,结束游戏
 93         System.out.println("退出游戏");
 94         // 调用展示结果的方法
 95         showResult();
 96     }
 97     
 98     public void showResult() {
 99         System.out.println("--------------------");
100         System.out.println(person.person_name + "   PK   " + computer.computer_name );
101         System.out.println("对战的次数:" + count);
102         
103         
104         System.out.println(person.person_name + "的得分:"+ person.person_score);
105         System.out.println(computer.computer_name + "得分"+computer.computer_score);
106         
107         if (person.person_score > computer.computer_score) {
108             System.out.println(person.person_name+ "好厉害");
109         }else if (person.person_score == computer.computer_score) {
110             System.out.println("最终是平局");
111         }else {
112             System.out.println("布要气馁,下次接着来");
113         }
114         
115         
116         
117     }
118     
119     
120     
121     
122     
123     
124 
125 }
Game类
public class GameTest {
    public static void main(String[] args) {
        Game game = new Game();
        
        game.startGame();
    }

}
GameTest类

 

标签:computer,猜拳,person,System,name,面向对象,小游戏,println,out
From: https://www.cnblogs.com/haizinihao/p/17150495.html

相关文章

  • 面向对象高级1-static&继承
    1,static1,static修饰成员变量static静态,可以修饰成员变量、成员方法。成员变量按照有无static修饰可以分为两种:类变量:属于类,与类一起加载一次,在内存中只有一份,可......
  • 面向对象编程
    1、面向对象编程1、特点封装:让使用对象的人不考虑内部实现,只考虑功能使用把内部的代码保护起来,只留出一些api接口供用户使用继承:就是为了代码的复用,从父类上继承出一......
  • 浅识面向对象和面向过程
    面向对象和面向过程的思想简介面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为。强调......
  • 【C语言】猜数字小游戏「功能优化」
    ......
  • 面向对象
    三大特性封装封装是把客观事物抽象成类,并且把自己的属性和方法让可信的类或对象操作,对不可性的隐藏。继承继承是指这样一种能力:它可以使用现有类的所有功能,并在无......
  • 面向对象(上)
    一、设计类 属性=成员变量=field=域、字段 方法=成员方法=函数=method 创建类的对象=类的实例化=实例化类二、类和对象的使用 1.创建类,设计类的成员 2.创建类......
  • 面向对象
    面向对象面向过程思想步骤简单清晰,第一步做什么,第二步做什么面向过程适合处理一些较为简单的问题面向对象思想物以类聚,分类的思维模式,思考问题首先会解决问题需要......
  • 面向对象类的概念
    前言在上一篇文章中,壹哥给大家介绍了面向对象和面向过程的概念,并介绍了两者的区别。但是上一篇文章中的内容,介绍的还是比较笼统。接下来壹哥会在本篇文章中,再单独给大家详......
  • golang 面向对象
    1.张老太养了两只猫:一只名字叫小白,今年3岁,白色。还有一只叫小花,今年100岁,花色。请编写一个程序,当用户输入小猫的名字时,就显示该猫的名字,年龄,颜色。如果用户输入的小猫名字......
  • 第十五章 面向对象程序设计
    第十五章面向对象程序设计OOP:概述面向对象程序设计(object-orientedprogramming)的核心思想是数据抽象、继承和动态绑定。继承(inheritance):通过继承联系在一起的类构......