java猜数游戏图形界面_Java做一个猜数的小游戏

@Author : By Runsen

效果展现

猜数字游戏是一个简单,有趣的小游戏。游戏者通过输入一个指定区间的数字,与系统产生的随机数进行对比,然后输出相应的结果。

游戏运行时产生一个0-10之间的随机整数,要求用户从控制台输入数字

若输入的数字比产生的数字小,则输出:“太小了,再大一点!”;

若输入的数字比产生的数字大,则输出:“太大了,再小一点!”,

若输入的数字和产生的数字相等,则输出:“恭喜你猜对了!”然后退出程序

388397b0d7f51a2ef6c3788cea17237d.png

功能要求

猜数字:生成一个指定范围内的随机正整数,从命令行读取一个整数,如果和随机数相同,就 算猜中。

固定随机数的范围

支持每次猜数字游戏的猜测次数,在指定次数内没猜对,则猜数字失败,否则就是成功。

可以支持退出游戏

输出剩余的猜测次数

每次猜测后,如果未猜中,则提示本次猜测的数字比目标数字大还是小

游戏结束后,输出猜数字游戏的统计

没有猜中,要输出这次的目标数字

可以设置随机数的范围,可以设置最大猜测次数

功能分析

第一,需要是设置猜数的范围,和次数,如果超过这个次数就直over了。

第二,还有统计每次游戏的次数和猜中游戏的次数

第三,如果输入-1就是结束游戏,因此需要一个变量判断是不是在运行游戏。

最后,就是需要通过 Math.random生成一个随机数,判断猜数是不是相等,相等就是猜中,没有就次数加一。

第一步

我就设置1到8,一共猜数的次数是五次,

// 创建Scanner从控制台读取输入Scanner in = new Scanner(System.in);// 游戏设置int rangeStart = 1;int rangeEnd = 8;int guessTotal = 5;

第二步

// 游戏统计int totalGameCount = 0;int totalCorrectCount = 0;

第三步

boolean stopGame = false;while (!stopGame) {    // 初始化本次游戏的变量    System.out.println("=================== 猜 数 字 ===================");    System.out.println("目标数已经生成,数字在" + rangeStart + "到" + rangeEnd + "之间," +            "不包括这两个数。共有" + guessTotal + "次猜测的机会。输入-1随时结束游戏。");    // 本次游戏是否开始了    boolean gameStart = true;    // 本次游戏是否开始了    boolean gameStart = true;    // 本次是否猜中数字    boolean guessCorrect = false;    while (guessLeft > 0) {}

最后一步

最后一步,一步的逻辑比较复杂,我直接上所有的代码

import java.util.Scanner;public class GuessNumber {    public static void main(String[] args) {        // 创建Scanner从控制台读取输入        Scanner in = new Scanner(System.in);        // 游戏设置        int rangeStart = 1;        int rangeEnd = 8;        int guessTotal = 5;        // 游戏统计        int totalGameCount = 0;        int totalCorrectCount = 0;        // 是否结束游戏        boolean stopGame = false;        while (!stopGame) {            // 初始化本次游戏的变量            // 游戏次数            int guessLeft = guessTotal;            // 区间            int mod = rangeEnd - rangeStart;            // 随机数 0到1的小数            double randNum = Math.random();            System.out.println(randNum);            // 求余数, 保证是0到8            int num = ((int) (randNum * rangeEnd * 100)) % mod;            System.out.println(num);            num += rangeStart;            System.out.println(num);            // 存在 num等于8或者1的清空            if (num <= rangeStart) {                num = rangeStart + 1;            }            if (num >= rangeEnd) {                num = rangeEnd - 1;            }            System.out.println("=================== 猜 数 字 ===================");            System.out.println("目标数已经生成,数字在" + rangeStart + "到" + rangeEnd + "之间," +                    "不包括这两个数。共有" + guessTotal + "次猜测的机会。输入-1随时结束游戏。");            // 本次游戏是否开始了            boolean gameStart = true;            // 本次是否猜中数字            boolean guessCorrect = false;            while (guessLeft > 0) {                System.out.println("还有" + guessLeft + "次机会,请输入猜测的数字,回车确认");                int guessNum = in.nextInt();                // 输入-1,结束游戏                if (guessNum == -1) {                    stopGame = true;                    break;                }                if (guessNum <= rangeStart || guessNum >= rangeEnd) {                    System.out.println("请输入在" + rangeStart + "到" + rangeEnd + "之间,的数字,不包括这两个数。");                    continue;                }                // 只要至少猜过一次,就算玩过了。                if (gameStart) {                    totalGameCount++;                    gameStart = false;                }                // 可以用的猜测次数减1                guessLeft--;                if (guessNum == num) {                    totalCorrectCount++;                    guessCorrect = true;                    System.out.println("恭喜你猜对了!这次的数字就是" + num +                            "。本次你共猜了" + (guessTotal - guessLeft) + "次。");                    break;                } else if (guessNum > num) {                    System.out.println("猜测的数字比目标数字大。");                } else {                    System.out.println("猜测的数字比目标数字小。");                }            }            if (!guessCorrect) {                System.out.println("本次的目标数字是" + num + "。这次没有猜中。");            }        }        System.out.println("共进行了" + totalGameCount + "次游戏,其中正确的为" + totalCorrectCount + "次");    }}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441186.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 266C】Below the Diagonal (递归,子问题,贪心模拟)

题干&#xff1a; You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1to n from left to right. Some cells (n - 1 cells in total) of the t…

python 0o_Python 中的比较:is 与 ==

在 Python 中会用到对象之间比较&#xff0c;可以用 &#xff0c;也可以用 is 。但是它们的区别是什么呢&#xff1f;is 比较的是两个实例对象是不是完全相同&#xff0c;它们是不是同一个对象&#xff0c;占用的内存地址是否相同。莱布尼茨说过&#xff1a;“世界上没有两片完…

python中long类型_浅谈python 四种数值类型(int,long,float,complex)

Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下。 数字数据类型存储数值。他们是不可改变的数据类型,这意味着改变数字数据类型的结果,在一个新分配的对象的值。 N…

【CodeForces - 264A】Escape from Stones (模拟,卡精度的处理)

题干&#xff1a; Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, nstones will fall and Liss will escape from the stones. The stones are nu…

python开发mbus程序_Python pywmbus包_程序模块 - PyPI - Python中文网

#WIP WM总线在Python中的实现本项目实施了无线m-bus标准的部分内容&#xff0c;定义见din en 13757-1及以下。目前&#xff0c;只支持未加密的短帧(即ci 0x7a)。欢迎拉取请求。##安装###点pip install pywmbus###手动git clone https://github.com/jalmeroth/pywmbus.gitcd pyw…

【CodeForces - 266B 】Queue at the School (模拟)

题干&#xff1a; During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in fr…

查看git当前tag_Git - git tag - 查看当前分支 tag 版本说明

索引&#xff1a;参看代码 GitHub&#xff1a;一、示例&#xff1a;1 git tag -l -n二、说明:1."tag" 部分tag 代表的是标签动作,可以带参数 ,也可以不带参数,带上不同的参数可以实现标签的 新建/删除/查询/验证 等功能.2."-l" 部分-l 注意是字母"L&q…

【CodeForces - 270A】Fancy Fence (几何,思维,水题)

题干&#xff1a; Emuskald needs a fence around his farm, but he is too lazy to build it himself. So he purchased a fence-building robot. He wants the fence to be a regular polygon. The robot builds the fence along a single path, but it can only make fenc…

判断集合相等_数学启蒙的每个关键阶段之集合分类

本文我们将分享数学启蒙学什么&#xff1f;用几个字简单的归纳为集合、数、量、形、时间、空间。我们接下来会讲感知集合和分类&#xff0c;数概念&#xff0c;量的概念&#xff0c;形状包含平面图形和立体图形&#xff0c;空间方位和时间的初步概念。 家长们可以发现幼儿数学启…

【CodeForces - 271B 】Prime Matrix (素数,预处理打表,思维)

题干&#xff1a; Youve got an n  m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary numb…

【CodeForces - 270C】Magical Boxes (思维,进制,有坑)

题干&#xff1a; Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes. From the top view each magical box looks like a square with side leng…

虚拟机安装黑群晖_【群晖系统】HEI群辉DSM 6.2.1 系统安装图文教程 (19年2月)

黑群晖系统其实是指在普通电脑运行Synology DSM系统, 事实上在普通PC电脑上安装黑群晖(Synology DSM)也非常方便, 现在把教程简单写一下。引导系统装哪里&#xff1f;非常关键的问题&#xff0c;DSM采用系统和数据相分离的结构&#xff0c;也就是说引导系统需要独立安装在一个设…

【Effect CodeForces - 270D】Greenhouse (思维,最长非递减子序列(上升),对偶问题,考虑反面)

题干&#xff1a; Emuskald is an avid horticulturist and owns the worlds longest greenhouse — it is effectively infinite in length. Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His …

错误1053服务没有及时_无法启动xx服务 错误1053:服务没有及时响应启动或控制请求,排查方法。...

sc安装服务&#xff0c;启动失败&#xff1a;显示错误1053&#xff1a;服务没有及时响应启动或控制请求网上找了很多方法资料&#xff0c;什么注册表啊&#xff0c;权限啊之类的。你按照这些都做完后&#xff0c;仍然提示这个错误。告诉你&#xff0c;要检查自己的程序是不是有…

【CodeForces - 219D 】Choosing Capital for Treeland (树形dp)

题干&#xff1a; The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we dont take the direction of the roads into consideration, we can get …

数据库分页查询mysql_各种数据库的分页查询SQL语句总结

1.oracle数据库分页select * from (select a.*,rownum rc from 表名 where rownum<endrow) a where a.rc>startrow2.DB2数据库分页Select * from (select rownumber() over() as rc,a.* from (select * from 表名 order by 列名) as a) where rc between startrow and en…

【HihoCoder - 1880】地铁环线 (前缀和,水题,模拟)

题干&#xff1a; H市有一环线地铁&#xff0c;一共包含N站&#xff0c;编号1~N。正向行驶的地铁会按1 -> 2 -> 3 -> ... -> N -> 1的方向行驶&#xff0c;反向会按1 -> N -> N-1 -> ... -> 3 -> 2 -> 1的方向行驶。 给定所有相邻两站之间…

unsigned int mysql_mysql 中int类型字段unsigned和signed的探索

转自&#xff1a;http://www.0791quanquan.com/news_keji/topic_816453/探索一&#xff1a;正负数问题拿tinyint字段来举例&#xff0c;unsigned后&#xff0c;字段的取值范围是0-255&#xff0c;而signed的范围是-128 - 127。 那么如果我们在明确不需要负值存在的情况下&#…

【HihoCoder - 1881】特殊任务 (树形图,遍历)

题干&#xff1a; H公司一共有N名员工&#xff0c;编号1~N&#xff0c;其中CEO是1号员工。除了CEO之外&#xff0c;其他员工都有唯一的直接上司&#xff0c;所以N名员工上下级关系恰好形成了一棵树形结构。 我们知道每一名员工向H公司的代码库贡献了多少行代码。具体来说&a…

mysql 主从 keepalived_MySQL之双向主从加keepalived高可用

最近在做MySQL数据库的双向主从&#xff0c;了解到keepalived能够自动判断并切换到可用数据库&#xff0c;自己试了一下&#xff0c;整理出文档来。先声明一下环境iptables开启3306端口或者关掉&#xff0c;关闭selinuxMySQL-01&#xff1a;192.168.204.138MySQL-02&#xff1a…