Java实现人机猜拳游戏
作者:梦小娴 时间:2023-10-16 08:47:56
本文实例为大家分享了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
标签:java,人机猜拳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django之多对多查询与操作方法详解
2021-08-03 03:21:58
![](https://img.aspxhome.com/file/2023/0/96060_0s.jpg)
详解android 中animation-list 动画的应用
2022-09-13 18:28:31
C# 获取动态key的json对象的值案例
2023-03-03 22:02:08
Java数据机构中关于并查集的详解
2022-09-05 13:23:53
![](https://img.aspxhome.com/file/2023/2/82982_0s.png)
Android 4.4.2 横屏应用隐藏状态栏和底部虚拟键的方法
2022-07-14 13:19:41
浅谈Springboot实现拦截器的两种方式
2023-05-10 05:53:50
![](https://img.aspxhome.com/file/2023/4/128864_0s.png)
谈谈为JAXB和response设置编码,解决wechat4j中文乱码的问题
2023-07-31 01:34:27
![](https://img.aspxhome.com/file/2023/1/57621_0s.png)
C#调用百度翻译实现翻译HALCON的示例
2022-06-08 23:33:50
![](https://img.aspxhome.com/file/2023/2/113812_0s.png)
FloatingActionButton增强版一个按钮跳出多个按钮第三方开源之FloatingActionButton
2023-06-18 13:28:22
![](https://img.aspxhome.com/file/2023/9/130379_0s.gif)
Java SpringMVC框架开发之数据导出Excel文件格式实例详解
2023-11-10 21:11:18
C#字符串加密解密方法实例
2022-01-11 23:30:14
C#数据结构与算法揭秘五 栈和队列
2023-09-13 13:31:47
![](https://img.aspxhome.com/file/2023/0/87030_0s.png)
JavaWeb ServletContext基础与应用详细讲解
2021-12-20 22:13:10
springboot-2.3.x最新版源码阅读环境搭建(基于gradle构建)
2022-11-19 01:47:41
![](https://img.aspxhome.com/file/2023/9/121929_0s.jpg)
Spring + Mybatis 项目实现动态切换数据源实例详解
2022-01-13 04:34:41
java实现单链表倒转的方法
2023-04-04 04:44:01
![](https://img.aspxhome.com/file/2023/8/94318_0s.jpg)
Android控件CardView实现卡片效果
2023-09-05 17:01:16
![](https://img.aspxhome.com/file/2023/8/126718_0s.jpg)
Spring Boot示例代码整合Redis详解
2022-03-20 19:23:37
Intellij IDEA根据maven依赖名查找它是哪个pom.xml引入的(图文详解)
2023-07-20 07:49:35
![](https://img.aspxhome.com/file/2023/4/100354_0s.jpg)
使用Java代码来比较Android客户端版本号
2021-09-07 17:44:22