类的无参方法(人机猜拳)
来源:互联网 发布:笔记本电脑软件打开慢 编辑:程序博客网 时间:2024/05/03 19:24
用户的类:<pre name="code" class="java">public class Computer {// 定义姓名和分数的对象String name;int score;public int show() {// 建立随机数int min = 1;int max = 3;int shu = (int) (Math.random() * max) + min;System.out.print("电脑出拳:");switch (shu) {case 1:System.out.println("剪刀");break;case 2:System.out.println("石头");break;case 3:System.out.println("布");break;}return shu;// 返回shu的值}}
电脑的类:
<pre name="code" class="java">public class Computer {// 定义姓名和分数的对象String name;int score;public int show() {// 建立随机数int min = 1;int max = 3;int shu = (int) (Math.random() * max) + min;System.out.print("电脑出拳:");switch (shu) {case 1:System.out.println("剪刀");break;case 2:System.out.println("石头");break;case 3:System.out.println("布");break;}return shu;// 返回shu的值}}游戏运行的类:
import java.util.*;public class Game {test ren;Computer computer;//创立对象String juese;//创立对象int name;int count;public void initial() {//对象的实例化computer = new Computer();ren = new test();}public void Gameshow() {initial();//调用initial方法boolean con;Scanner input = new Scanner(System.in);System.out.print("请输入你的姓名:");juese = input.next();do {con = true;System.out.println("************欢迎进入游戏世界**************\n\n");System.out.println("\t\t************************");System.out.println("\t\t*** 猜拳开始 ***");System.out.println("\t\t************************\n\n\n");System.out.println("出拳规则:1.剪刀 2.石头 3.布");System.out.println("请选择对方角色:(1.刘备 2.曹操 3.孙权)");name = input.nextInt();switch (name) {case 1:computer.name = "刘备";break;case 2:computer.name = "曹操";break;case 3:computer.name = "孙权";break;}System.out.print(computer.name);System.out.println();Scanner input1 = new Scanner(System.in);System.out.println("要开始吗?(y/n)");String can = input1.next();int renfist;int comfist;if ("y".equals(can)) {/** * @return人的返回值 */renfist = ren.showren();/** * @return电脑的返回值 */comfist = computer.show();//将人的返回值和电脑的返回值进行比较来判断谁输谁赢if ((renfist == 1 && comfist == 1)|| (renfist == 2 && comfist == 2)|| (renfist == 3 && comfist == 3)) {System.out.println("结果:和局!\n");} else if ((renfist == 1 && comfist == 3)|| (renfist == 2 && comfist == 1)|| (renfist == 3 && comfist == 2)) {System.out.println("结果:你赢了!\n");ren.score++;//计算人赢得次数} else {System.out.println("结果:你输了!\n");computer.score++;//计算电脑赢得次数}count++;//计算总次数} else {con = false;//跳出循环}} while (con);System.out.println("----------------------------");System.out.println(computer.name + "VS" + juese);System.out.println("对战次数" + count);System.out.println(juese + "赢的次数: " + ren.score +computer.name +"赢的次数:" + computer.score);//将人的分数和电脑的分数进行比较if (computer.score == ren.score) {System.out.println("平局");} else if (computer.score < ren.score) {System.out.println(juese + "赢了" + computer.name + "输了");} else {System.out.println(computer.name + "赢了" + juese + "输了");}}}
调用游戏的类:
public class kai {public static void main(String[] args) {Game de = new Game();de.Gameshow();}}
0 0
- 类的无参方法(人机猜拳)
- 人机猜拳小游戏(面向对象的方法)
- 人机互动版猜拳游戏(面向对象方法实现)
- 简单的人机猜拳小游戏
- 人机猜拳游戏(修改)
- 人机猜拳
- 人机猜拳
- 人机猜拳
- 人机猜拳
- 人机猜拳
- 人机猜拳
- 人机猜拳
- 基于java的人机猜拳游戏
- java简单的人机猜拳小游戏
- Java人机猜拳游戏(命令行版)
- 人机猜拳游戏
- 第十二章 人机猜拳
- Java_人机猜拳游戏
- Magento(1.*)Events 事件一览
- 80端口被占用
- 学习笔记(九)——传感器与LBS
- 如何实现一台IPC摄像机添加至多台录像机
- 安卓 CSS absolute 兼容性问题
- 类的无参方法(人机猜拳)
- 时间格式化转换
- js学习笔记:引用类型——Object
- java字符串
- java的concurrent用法详解
- nginx学习(九)——nginx的配置系统5之rewrite
- Unity - 杂 - 使用Linear还是Gamma
- tomcat配置SSL双向认证
- Androidstudio-framwork开发MTK平台,导入关联源码及AS一些相关设置