Java控制台实现猜拳游戏
作者:一本正经小崽崽 时间:2022-12-15 09:54:46
本文实例为大家分享了Java控制台实现猜拳游戏的具体代码,供大家参考,具体内容如下
1、目标
通过控制台实现一个人机对战的猜拳游戏,用户通过输入(1.剪刀 2.石头 3.布),机器随机生成(1.剪刀 2.石头 3.布),胜者积分,n 局以后通过积分的多少判定胜负。
2、源码
根据目标的要求,可以大概知道,我们需要的类有:
①一个用户类User,用于获得和存储用户的出拳;
②一个机器人的类Robot,用于随机生成机器人的出拳;
③一个Game类,是存放核心代码的类,用户和机器人的出拳在这里进行对比,对结果进行记录,最后输出结果;
④最后是一个调用各个方法的Guess类。
直接上代码:
User.java
import java.util.Scanner;
public class User{
static Scanner input = new Scanner(System.in);
int user = 0;//用户出拳
int score = 0;//用户积分
int num = 0;//对战局数
public int setUser(){
System.out.println("请输入:1.剪刀 2.石头 3.布");
user = input.nextInt();
if (user == 1){
System.out.println("用户出拳:剪刀");
}else if (user == 2){
System.out.println("用户出拳:石头");
}else if (user == 3){
System.out.println("用户出拳:布");
}
return user;
}
}
Robot.java
import java.util.Random;
public class Robot {
int id = 0;//获得机器人的出手
int score = 0;//机器人的积分
//获得机器人的出拳
public int setId(){
Random random = new Random();
int r = random.nextInt(3);
id = r + 1;//获取机器人随机出拳
if (id == 1){
System.out.println("机器人出拳:剪刀");
}else if (id == 2){
System.out.println("机器人出拳:石头");
}else if (id == 3){
System.out.println("机器人出拳:布");
}
return id;
}
}
Game.java
import java.util.Scanner;
public class Game {
//对用户类和机器人类进行初始化
User user = new User();
Robot robot = new Robot();
public void GameStart() {
//初始化界面
System.out.println("-------------欢迎进入猜拳游戏---------------");
System.out.println();
System.out.println("******************************************");
System.out.println("——————出拳规则:1.剪刀 2.石头 3.布——————————");
System.out.println("-------------》》》猜拳开始《《《------------");
System.out.println("-------------------------------------------");
Scanner input = new Scanner(System.in);
//获得用户想要进行的对战次数
System.out.println("请输入对战局数:");
user.num = input.nextInt();
System.out.println("游戏开始");
int userFirst;//用户出拳
int robotFirst;//机器人出拳
for (int i = 0; i < user.num; i++) {
//获取双方的出拳
userFirst = user.setUser();
robotFirst = robot.setId();
//判断胜负
if (userFirst == robotFirst) {
System.out.println("结果:平局,不积分。");
} else if ((userFirst == 2 && robotFirst == 1) || (userFirst == 3 && robotFirst == 2)) {
System.out.println("结果:你赢了,加一分!");
user.score++;
} else {
System.out.println("结果:你输了,机器人加一分。");
robot.score++;
}
}
//显示结果
showResult();
}
//输出对战结果
private void showResult() {
//显示对战次数
System.out.println("------------------------------");
System.out.println("对战次数:" + user.num);
//显示最终得分
System.out.println("\n姓名\t得分");
System.out.println("用户" + "\t" + user.score);
System.out.println("机器人" + "\t" + robot.score + "\n");
//显示对战结果
int result = Result();
if (result == 1) {
System.out.println("结果:打成平手。");
} else if (result == 2) {
System.out.println("结果:恭喜你赢得对战!");//用户获胜
} else {
System.out.println("结果:你输了,不气馁,下次再来");//计算机赢
}
System.out.println("游戏结束");
System.out.println("--------------------------");
}
//计算比赛结果
private int Result() {
if (user.score == robot.score) {
return 1;
} else if (user.score > robot.score) {
return 2;
} else {
return 3;
}
}
}
Guess.java
public class Guess {
public static void main(String[] args) {
Game game = new Game();
//调用输出比赛结果的方法
game.GameStart();
}
}
来源:https://blog.csdn.net/weixin_45573289/article/details/112750882
标签:java,猜拳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
intellij idea14打包apk文件和查看sha1值
2022-05-25 13:18:37
![](https://img.aspxhome.com/file/2023/6/91746_0s.jpg)
java实现上传图片尺寸修改和质量压缩
2023-04-04 03:42:55
javaweb上传下载实例完整版解析(上)
2021-07-31 05:40:23
![](https://img.aspxhome.com/file/2023/4/63114_0s.jpg)
Spring Cloud Alibaba 整合Nacos的详细使用教程
2022-10-19 22:18:00
![](https://img.aspxhome.com/file/2023/6/119776_0s.png)
SpringMVC @NotNull校验不生效的解决方案
2021-07-19 23:20:07
Java date format时间格式化操作示例
2021-10-28 19:12:24
java线程间通信的通俗解释及代码示例
2023-12-06 18:04:43
如何使用RequestHeaders添加自定义参数
2023-01-10 17:37:14
![](https://img.aspxhome.com/file/2023/9/105949_0s.png)
Android对话框AlertDialog详解
2023-06-20 01:47:19
![](https://img.aspxhome.com/file/2023/3/82423_0s.jpg)
FFmpeg 音频可视化解码流程详解
2023-04-03 00:41:54
![](https://img.aspxhome.com/file/2023/4/131864_0s.png)
MyBatis @Select注解介绍:基本用法与动态SQL拼写方式
2023-07-17 05:56:43
Java构造函数的相互调用代码示例
2023-07-01 21:42:11
Spring实战之Bean定义中的SpEL表达式语言支持操作示例
2021-07-07 10:46:19
C#实现简单获取及设置Session类
2021-07-09 06:19:27
浅入浅出的讲解Spring循环依赖问题
2023-11-03 07:16:11
![](https://img.aspxhome.com/file/2023/5/79285_0s.png)
详解WMI RPC 服务器不可用的解决方案
2023-09-14 14:51:54
SpringBoot中的yaml语法及静态资源访问问题
2021-09-27 20:32:38
![](https://img.aspxhome.com/file/2023/6/86846_0s.png)
hadoop实现grep示例分享
2023-12-24 05:31:04
Java实现二叉堆、大顶堆和小顶堆
2022-08-20 18:39:33
![](https://img.aspxhome.com/file/2023/2/63772_0s.png)
DataGridView实现点击列头升序和降序排序
2022-07-31 21:54:01
![](https://img.aspxhome.com/file/2023/0/129660_0s.jpg)