版本1:人机大战  基础随机出    用户键盘录入

package com.hainiu.demo;

import java.util.Scanner;

/*
* 人机大战石头剪刀布
*/
public class Cycles { public static void main(String[] args) {
while(true){
System.out.println("-----欢迎来到游戏界面----");
System.out.println("1:剪刀、2:石头、3:布");
Scanner sc = new Scanner(System.in);
int person = sc.nextInt();
int computer = (int)(Math.random()*(3)+1);
String per="用户";
String com="电脑";
//用户
switch(person){
case 1:
per="剪刀";
break;
case 2:
per="石头";
break;
case 3:
per="布";
break;
//电脑
}
switch(computer){
case 1:
com="剪刀";
break;
case 2:
com="石头";
break;
case 3:
com="布";
break;
//判断
}
if(person==1&&computer==2||person==2&&computer==3||person==3&&computer==1){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("你输啦");
}else if(person==2&&computer==1||person==3&&computer==2||person==1&&computer==3){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("你赢了");
}else if(person==computer){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("平局");
}else{
System.out.println("您的输入有误");
break;
}
}
}
}

运行结果:

-----欢迎来到游戏界面----
1:剪刀、2:石头、3:布
2
你出的是: 石头   电脑出的是: 剪刀
你赢了
-----欢迎来到游戏界面----
1:剪刀、2:石头、3:布

版本2:

猜拳游戏说明:

⦁    任务
⦁    完成人机猜拳互动游戏的开发
⦁    主要功能
⦁    选取对战角色
⦁    猜拳
⦁    记录分数
⦁    需求说明
⦁    分析业务
⦁    抽象出类、类的特征和行为
⦁    实现思路:
⦁    分析业务,抽象出类、类的特征和行为

package com.hainiu.demo;

import java.util.Scanner;

class User{
Scanner sc = new Scanner(System.in);
private String name;
private int integral;
private String punch;
public User() {
// TODO Auto-generated constructor stub
}
public User(String name, int integral, String punch) {
super();
this.name = name;
this.integral = integral;
this.punch = punch;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
public String getPunch() {
return punch;
}
public void setPunch(String punch) {
this.punch = punch;
}
//用户输入名称
public void inputName(){
System.out.println("请输入用户录入名");
name = sc.next();
}
//猜拳方法
public void UserGuess(){ int n= sc.nextInt();
System.out.println("1、剪刀,2、石头、3、布");
if(n>0&&n<=3){
switch (n) {
case 1:
this.punch="剪刀";
break;
case 2:
this.punch="石头";
break;
case 3:
this.punch="布";
break;
}
}else{
System.out.println("输入有误");
} }
}
class Computer{
Scanner sc = new Scanner(System.in);
private String name;
private int integral;
private String punch;
public Computer() { }
public Computer(Scanner sc, String name, int integral, String punch) {
this.sc = sc;
this.name = name;
this.integral = integral;
this.punch = punch;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
public String getPunch() {
return punch;
}
public void setPunch(String punch) {
this.punch = punch;
}
//选择用户
public void computerName(){
System.out.println("请选择您的对手:");
System.out.println("1、扫地僧,2、逍遥子、3、独孤求败");
int n = sc.nextInt();
switch (n) {
case 1:
this.name="扫地僧";
break;
case 2:
this.name="逍遥子";
break;
case 3:
this.name="独孤求败";
break;
}
}
//猜拳方法
public void comuterGuess(){
//System.out.println("对手出的是:");
int t = (int)(Math.random()*(3)+1);
switch (t) {
case 1:
this.punch="剪刀";
break;
case 2:
this.punch="石头";
break;
case 3:
this.punch="布";
break;
}
}
}
class TestGame{
User u = new User();
Computer c =new Computer();
//欢迎进入游戏菜单
public void start(){
//初始化对象
System.out.println("欢迎进入游戏菜单");
System.out.println("游戏开始");
//选择您的对手
c.computerName();
u.inputName();
System.out.println(u.getName()+"vs"+c.getName());
}
//判断出拳的结果
public void game(){
u.UserGuess();;
c.comuterGuess();
System.out.println("你出的是"+u.getPunch());
System.out.println(c.getName()+"出的是"+c.getPunch());
//具体判断输赢的 剪刀 石头 布
if(u.getPunch().equals("剪刀") && c.getPunch().equals("石头")||u.getPunch().equals("石头") && c.getPunch().equals("布") || u.getPunch().equals("布") && c.getPunch().equals("剪刀")){
c.setIntegral(c.getIntegral()+1);
System.out.println("你输了");
}else if(u.getPunch().equals("石头")&&c.getPunch().equals("剪刀")||u.getPunch().equals("布")&&c.getPunch().equals("石头")||u.getPunch().equals("剪刀")&&c.getPunch().equals("布")){
u.setIntegral(u.getIntegral()+1);
System.out.println("你赢啦");
}else {
System.out.println("平局");
}
} public void last(){
System.out.println("最后结果是:");
System.out.println(u.getName()+"赢了"+u.getIntegral()+"局");
System.out.println(c.getName()+"赢了"+c.getIntegral()+"局");
if(u.getIntegral()>c.getIntegral()){
System.out.println("算总分"+u.getName()+"或的最后的胜利");
}else if(u.getIntegral()<c.getIntegral()){
System.out.println("算总分"+c.getName()+"或的最后的胜利");
}else if(u.getIntegral()==c.getIntegral()){
System.out.println("最后平局");
}else {
System.out.println("输入有误,这不是系统的问题");
}
}
}
public class Cycles2 { public static void main(String[] args) {
// 测试
Scanner sc = new Scanner(System.in);
System.out.println("---------游戏开始----------\n");
TestGame tg = new TestGame();
tg.start();
System.out.println("输入y继续,其他人任意键结束");
String panduan = sc.next();
//循环game方法
while(panduan.equals("y")){
System.out.println("******************");
System.out.println("1、剪刀,2、石头、3、布");
tg.game();
System.out.println("y:继续-任意:结束");
panduan = sc.next();
}
System.out.println("游戏结束");
tg.last();
} }

java--demo之猜拳游戏的更多相关文章

  1. 有趣的java小项目------猜拳游戏

    package com.aaa; //总结:猜拳游戏主要掌握3个方面:1.人出的动作是从键盘输入的(System.in)2.电脑是随机出的(Random随机数)3.双方都要出(条件判断) import ...

  2. python与java的猜拳游戏

    python版: import randomprint("-----猜拳游戏-----")print("---0.剪刀--1.石头--2.布---")while ...

  3. 人机猜拳游戏Java

    作业要求: 我的代码: package day20181119;/** * 猜拳游戏 * @author Administrator * @version1.0 */import java.util. ...

  4. Java 入门课程视频实战-0基础 上线了,猜拳游戏,ATM实战,欢迎围观

    Java 入门课程视频实战-0基础 已经上传完了.欢迎小伙伴们过来围观 直接进入: http://edu.csdn.net/course/detail/196 课程文件夹例如以下: 1 初识Java  ...

  5. 猜拳游戏三局两胜------java实现代码

    package com.javasm.exerices02; import java.util.ArrayList; import java.util.List; import java.util.R ...

  6. Java中利用随机数的猜拳游戏

    Java中利用随机数的猜拳游戏,实现非常简单,重难点在于随机数的产生. 首先GameJude类是用于判断输赢的一个类: package testGame; public class GameJudge ...

  7. java 人机猜拳 游戏

    人机猜拳-游戏 掌握类和对象的使用,掌握方法的定义和返回值,掌握封装的运用 定义一个电脑类:Computer.java 点击查看[Computer.java]代码 /** * @Title: 电脑类 ...

  8. JAVA 猜拳游戏

    JAVA 猜拳游戏 题目:通过控制台方式实现一个人机对战的猜拳游戏 用户通过输入(0.石头子 1.剪刀 2.布),机器随机生成(0.石头子 1.剪刀 2.布) 要求: 能打印玩家的对局信息,胜利的次数 ...

  9. 用Java编写的猜拳小游戏

    学习目标: 熟练掌握各种循环语句 例题: 代码如下: // 综合案例分析,猜拳案例 // isContinue为是否开始游戏时你所输入的值 char isContinue; //y为开始,n为借宿 S ...

随机推荐

  1. selenium鼠标操作

    #-*- coding:utf-8 -*- import time from selenium import webdriver from selenium.webdriver.common.acti ...

  2. Tkinter 之Canvas画布

    一.参数说明 参数 作用 background(bg) 指定 Canvas 的背景颜色 borderwidth(bd) 指定 Canvas 的边框宽度 closeenough 指定一个距离,当鼠标与画 ...

  3. Docker实战笔记

    好记性不如烂笔头,持续高产~ 0x01 Docker创建nginx容器 1.流程 docker info 显示Docker系统信息,包括镜像和容器数 示例: 使用镜像 nginx:latest 以交互 ...

  4. [RK3288] 外接USB设备出现丢数

    CPU:RK3288 系统:Android 5.1 主板外接 USB 接口的外设,经常会出现丢数的现象,这种问题在很多 USB 接口的外设上都遇到过,例如:USB读卡器.USB扫描枪等 有一个共同点是 ...

  5. REST和SOAP的区别

    转自:https://www.cnblogs.com/MissQing/p/7240146.html REST似乎在一夜间兴起了,这可能引起一些争议,反对者可以说REST是WEB诞生之始甚而是HTTP ...

  6. 控制 Python 工具箱中的许可行为

    def isLicensed(self): """Allow the tool to execute, only if the ArcGIS 3D Analyst 扩展模 ...

  7. ThinkPhp5 mongodb 使用自定义objectID出错解决

    在Tp5中使用mongodb 使用自定义ObjectId时报错:Cannot use object of type MongoDB\\BSON\\ObjectID as array 查询源码发现在to ...

  8. QML小例子【QML工程里信号与槽】

    1.效果 代码参考B站视频:https://www.bilibili.com/video/av36584062 功能:点击左边,会发出信号,右边会有个颜色动画,然后计数+1 2.分析: 一共有两个对象 ...

  9. php上传超大文件

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  10. 微信小程序企业付款到个人

    <?php /** * 小程序之企业付款到个人! */ class WxPayModel extends Model { public function sendMoneyToPerson($t ...