Java实现人机猜拳游戏
作者:梦小娴 发布时间:2023-10-16 08:47:56
标签:java,人机猜拳
本文实例为大家分享了Java实现人机猜拳游戏的具体代码,供大家参考,具体内容如下
实现:
User类
public class User
{
private String name;
private int score=0;
private int num;
public String GetName()
{
return this.name;
}
public void SetName(String name)
{
this.name=name;
}
public int GetScore()
{
return this.score;
}
public void SetScore(int score)
{
this.score+=score;
}
}
Computer类
public class Computer
{
private String name;
private int score=0;
private int num;
public String GetName()
{
return this.name;
}
public void SetName(String name)
{
this.name=name;
}
public int RandNums()
{
int n;
n=(int)(Math.random()*3)+1;// 返回1到3的随机整数。
return n;
}
public int GetScore()
{
return this.score;
}
public void SetScore(int score)
{
this.score+=score;
}
}
Gamemanager类
import java.util.Scanner;
public class GameManager
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);//创建一个键盘扫描类对象
User user=new User();
Computer computer=new Computer();
int vsNums=0;
System.out.println("出拳游戏规则:1、剪刀,2、石头,3、布");
System.out.println("请选择对方角色(1、刘备,2、孙权,3、曹操)");
int n=input.nextInt(); //输入整型
switch(n)
{
case 1:
computer.SetName("刘备");
break;
case 2:
computer.SetName("孙权");
break;
case 3:
computer.SetName("曹操");
break;
}
System.out.println("请输入你的姓名");
String name=input.next(); //输入字符串型
user.SetName(name);
System.out.println(user.GetName()+" "+"VS"+" "+computer.GetName());
String flag="y";
while(flag.equals(flag))
{
System.out.println("要开始吗y/n");
String yOrn=input.next(); //输入字符串型
if(yOrn.equals("y"))
{
vsNums++;
System.out.println("请出拳:1、剪刀,2、石头,3、布(输入数字)");
int nums=input.nextInt(); //输入整型
switch(nums)
{
case 1:
System.out.println("你出拳:"+"剪刀");
break;
case 2:
System.out.println("你出拳:"+"石头");
break;
case 3:
System.out.println("你出拳:"+"布");
break;
}
int rand=computer.RandNums();
switch(rand)
{
case 1:
System.out.println(computer.GetName()+"出拳:"+"剪刀");
break;
case 2:
System.out.println(computer.GetName()+"出拳:"+"石头");
break;
case 3:
System.out.println(computer.GetName()+"出拳:"+"布");
break;
}
if(nums==1 && rand==3 || nums==2 && rand==1 || nums==3 && rand==2)
{
System.out.println("恭喜,你赢了");
user.SetScore(1);
}
else if(nums==rand)
{
System.out.println("平手了");
}
else
{
System.out.println("很遗憾,你输了");
computer.SetScore(1);
}
}
else
{
System.out.println(computer.GetName()+" "+"VS"+" "+user.GetName());
System.out.println("对战次数:"+vsNums);
System.out.println("姓名\t得分");
System.out.println(user.GetName()+"\t"+user.GetScore());
System.out.println(computer.GetName()+"\t"+computer.GetScore());
if(user.GetScore()>computer.GetScore())
{
System.out.println("恭喜,恭喜");
}
else
{
System.out.println("继续加油");
}
break;
}
}
}
}
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏 玩不停
java经典小游戏汇总
javascript经典小游戏汇总
来源:https://blog.csdn.net/weixin_44350205/article/details/107600296


猜你喜欢
- 上一篇博文《Android中Handler使用浅析》通过实现倒计时闪屏页面的制作引出了Handler的使用方法以及实现原理,博文末尾也提到了
- 使用方法:先把mvcpager.dll引用加入mvc项目中。前台代码前台:@{Layout = null;}@using Webdiyer.
- 前言去年7月时,在Github发布了一个开源的Banner库,虽然Star不多,但还是有少部分人使用。Banner效果:Github链接地址
- 一、strcmp函数适用对象char*类型字符串函数介绍strcmp函数是cstring库中的函数,包含在string.h头文件中用法str
- 我们开发任何一个Spring Boot项目,都会用到如下的启动类@SpringBootApplication public class Ap
- 一.Unsafe类的源码分析JDK的rt.jar包中的Unsafe类提供了硬件级别的原子操作,Unsafe里面的方法都是native方法,通
- 本文实例讲述了Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用。分享给大家供大家参考,具体如下:现在我
- 本文实例讲述了Android实现在xml文件中引用自定义View的方法。分享给大家供大家参考,具体如下:在xml中引用自定义view方法一:
- 本文实例讲述了java实现简单的英文文本单词翻译器功能。分享给大家供大家参考,具体如下:直接上代码:package fanyi;import
- 使用包管理器package com.liunan.myfirstapp.util;import android.content.Contex
- * 缓存内存缓存本地缓存(SD卡缓存)网络缓存缓存顺序:首先从网络获取图片资源,然后将当前的图片缓存到本地,然后再缓存到内存中,那么下次访问
- android 实现拨打电话的app,代码非常简单,功能也很实用,分享给大家。MainActivity.javapackage com.bb
- 最近项目中遇到一个问题,在用户没填数据的时候,我们需要接收从前端传过来的对象为null,但是前端说他们一个一个判断特别麻烦,只能传个空对象过
- 本文实例讲述了安卓Android6.0权限动态获取操作。分享给大家供大家参考,具体如下:众所周知 , 安卓6.0现在运用的越来越广泛 , 因
- 今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查、分页、排序、事务操作等功能。下面先来介绍一下JPA中一些常用的
- Android 列表组件 ListView列表组件是开发中经常用到组件,使用该组件在使用时需要为它提供适配器,由适配器提供来确定显示样式和显
- 一、单文件压缩 场景,文件可能比较大,需要压缩传输,比如上传和下载/// <summary&g
- 平常我们工作中基本最多两级嵌套,但是有时候难免会遇到 * 嵌套的业务场景,笔者最近就碰到了,使用一般的嵌套发现赋值为空,这可难倒了菜逼的我,后
- 文件上传概述实现web开发中的文件上传功能,需完成如下二步操作:在web页面中添加上传输入项在servlet中读取上传文件的数据,并保存到本
- hdfs上的文件是手动执行命令从本地linux上传至hdfs的。在真实的运行环境中,我们不可能每次手动执行命令上传的,这样太过繁琐。那么,我