java 猜拳游戏代码分享
来源:互联网 发布:ubuntu退出命令行界面 编辑:程序博客网 时间:2024/05/22 12:00
面向对象的思维去理解
package com.game.guess;/** * 计算机类 * */public class Computer { String name = "电脑"; // 名字 int score = 0;; // 积分 /** * 出拳 * @return 出拳结果:1.剪刀 2.石头 3.布 */ public int showFist(){ // 产生随机数 int show = (int)(Math.random()*10)%3 + 1; //产生随机数,表示电脑出拳 // 输出出拳结果并返回 switch(show){ case 1: System.out.println(name+"出拳: 剪刀"); break; case 2: System.out.println(name+"出拳: 石头"); break; case 3: System.out.println(name+"出拳: 布"); break; } return show; }}
package com.game.guess;import java.util.Scanner;/** * 用户类 * */public class Person { String name = "匿名"; // 名字 int score = 0; // 积分 /** * 出拳 * @return 出拳结果:1.剪刀 2.石头 3.布 */ public int showFist(){ // 接收用户的选择 Scanner input = new Scanner(System.in); System.out.print("\n请出拳:1.剪刀 2.石头 3.布 (输入相应数字) :"); int show = input.nextInt(); // 输出出拳结果,并返回 switch(show){ case 1: System.out.println("你出拳: 剪刀"); break; case 2: System.out.println("你出拳: 石头"); break; case 3: System.out.println("你出拳: 布"); break; } return show; }}
package com.game.guess;import java.util.Scanner;/** * 游戏类 :实现对战结果显示 */public class Game {Person person; // 甲方Computer computer; // 乙方int count; // 对战次数/** * 初始化 */public void initial() {person = new Person();computer = new Computer();count = 0;}/** * 开始游戏 */public void startGame() {initial(); // 初始化System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------\n");System.out.println("\n\t\t******************");System.out.println("\t\t** 猜拳, 开始 **");System.out.println("\t\t******************");System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");/* 选择对方角色 */System.out.print("请选择对方角色(1:乔布斯 2:比尔盖茨 3:马云): ");Scanner input = new Scanner(System.in);int role = input.nextInt();if (role == 1) {computer.name = "刘备";} else if (role == 2) {computer.name = "孙权";} else if (role == 3) {computer.name = "曹操";}System.out.println("你选择了 " + computer.name + "对战");System.out.print("\n要开始吗?(y/n) ");String con = input.next();int perFist; // 用户出的拳int compFist; // 计算机出的拳while (con.equals("y")) {/* 1)出拳 */perFist = person.showFist();compFist = computer.showFist();/* 2)裁决 */if ((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)) {System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 !\n");// 平局} else if ((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1)|| (perFist == 3 && compFist == 2)) {System.out.println("结果: 恭喜, 你赢了!"); // 用户赢person.score++;} else {System.out.println("结果说:^_^,你输了,真笨!\n"); // 计算机赢computer.score++;}count++;// 3)判断,是否要继续游戏System.out.print("\n是否开始下一轮(y/n): ");con = input.next();}/* 显示结果 */showResult();}/** * 显示比赛结果 */public void showResult() {/* 显示最后结果 */System.out.println("---------------------------------------------------");System.out.println(computer.name + " VS " + person.name);System.out.println("对战次数:" + count);int result = calcResult();if (result == 1) {System.out.println("结果:打成平手,下次再和你一分高下!");} else if (result == 2) {System.out.println("结果:恭喜恭喜!"); // 用户获胜} else {System.out.println("结果:呵呵,笨笨,下次加油啊!"); // 计算机获胜}System.out.println("---------------------------------------------------");}/** * 计算比赛结果 * * @return 1:战平;2:用户赢;3:电脑赢 */public int calcResult() {if (person.score == computer.score) {return 1; // 战平} else if (person.score > computer.score) {return 2; // 用户赢} else {return 3; // 电脑赢}}}
package test;import com.game.guess.Game;/** * 人机互动版猜拳游戏 * 程序入口 */public class StartGuess {public static void main(String[] args) {Game Game= new Game();Game.startGame();}}
阅读全文
0 0
- java 猜拳游戏代码分享
- java 猜拳游戏代码
- java 猜拳游戏代码
- java猜拳游戏
- 猜拳游戏Java
- 【JAVA程序】猜拳游戏
- JAVA 猜拳游戏三局两胜版
- JAVA 猜拳游戏
- Java-猜拳游戏
- Java猜拳游戏
- java实现猜拳游戏
- java猜拳游戏
- Java-猜拳游戏
- JAVA 猜拳游戏
- JAVA编写的猜拳游戏
- JAVA简单猜拳游戏(二)
- JAVA简单猜拳游戏(三)
- java实现的 猜拳游戏
- Python2与Python3在不同类的对象比较上的差异
- Linux下统计当前文件夹下的文件个数、目录个数
- 并查集 版子
- XAMPP下MYSQL存中文出现乱码的解决方法
- Android selinux权限
- java 猜拳游戏代码分享
- Python中的random模块
- brew安装tomcat
- C语言的指针
- SpringMVC的工作流程
- 浅谈安全模式
- hdu 1851 A Simple Game
- hdu 4004 贪心 二分查找
- sort函数