代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<!-- saved from url=(0041)http://c.biancheng.net/view/vip_7089.html -->
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" async="" charset="utf-8" src="./13.12Java项目实战:猜数字小游戏_files/core.php"></script>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<!-- 启用Chromium高速渲染模式 -->
<meta name="renderer" content="webkit">
<meta name="force-rendering" content="webkit">
<!-- 禁止百度转码 -->
<meta name="applicable-device" content="pc,mobile">
<meta name="MobileOptimized" content="width">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<!-- 禁止识别电话号码 -->
<meta name="format-detection" content="telephone=no">
<link rel="shortcut icon" href="http://c.biancheng.net/favicon.ico?v=1.6.72">
<link href="./13.12Java项目实战:猜数字小游戏_files/common.css" rel="stylesheet">
<title>Java项目实战:猜数字小游戏</title>
<meta name="description" content="猜数字是一个经典的小游戏,程序先产生一个随机数,然后用户输入数字,程序将输入的数字与随机数进行对比,给出用户相应的提示信息。 本节实现了一个基于 IO 流的猜数字游戏,游">
</head>
<body>
<div id="topbar" class="clearfix">
<ul id="product-type" class="left">
<li>
<a href="http://c.biancheng.net/"><span class="iconfont iconfont-home"></span>首页</a>
</li>
<li class="active">
<a href="http://c.biancheng.net/sitemap/" rel="nofollow"><span class="iconfont iconfont-book"></span>教程</a>
</li>
<li>
<a href="http://vip.biancheng.net/p/vip/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-vip"></span>VIP会员</a>
</li>
<li>
<a href="http://vip.biancheng.net/p/q2a/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-q2a"></span>一对一答疑</a>
</li>
<li>
<a href="http://fudao.biancheng.net/" rel="nofollow" target="_blank"><span class="iconfont iconfont-fudao"></span>辅导班</a>
</li>
</ul>
</div>
<div id="header" class="clearfix">
<a id="logo" class="left" href="http://c.biancheng.net/">
<img height="26" src="./13.12Java项目实战:猜数字小游戏_files/logo.png" alt="C语言中文网">
</a>
<ul id="nav-main" class="hover-none left clearfix">
<li class="wap-yes"><a href="http://c.biancheng.net/">首页</a></li>
<li><a href="http://c.biancheng.net/c/">C语言教程</a></li>
<li><a href="http://c.biancheng.net/cplus/">C++教程</a></li>
<li><a href="http://c.biancheng.net/python/">Python教程</a></li>
<li><a href="http://c.biancheng.net/java/">Java教程</a></li>
<li><a href="http://c.biancheng.net/linux_tutorial/">Linux入门</a></li>
<li><a href="http://c.biancheng.net/sitemap/" title="网站地图">更多>></a></li>
</ul>
<span id="sidebar-toggle" class="toggle-btn" toggle-target="#sidebar">目录 <span class="glyphicon"></span></span>
<a href="http://vip.biancheng.net/?from=topbar" class="user-info glyphicon glyphicon-user hover-none" target="_blank" rel="nofollow" title="用户中心"></a>
</div>
<div id="main" class="clearfix">
<div id="sidebar" class="toggle-target">
<div id="contents">
<dt><span class="glyphicon glyphicon-option-vertical" aria-hidden="true"></span><a href="http://c.biancheng.net/java/">Java教程</a></dt>
<dd>
<span class="channel-num">1</span>
<a href="http://c.biancheng.net/java/10/">Java入门基础及环境搭建</a>
</dd>
<dd>
<span class="channel-num">2</span>
<a href="http://c.biancheng.net/java/20/">Java程序设计基础</a>
</dd>
<dd>
<span class="channel-num">3</span>
<a href="http://c.biancheng.net/java/30/">Java流程控制语句</a>
</dd>
<dd>
<span class="channel-num">4</span>
<a href="http://c.biancheng.net/java/40/">Java字符串处理</a>
</dd>
<dd>
<span class="channel-num">5</span>
<a href="http://c.biancheng.net/java/50/">Java数字和日期处理</a>
</dd>
<dd>
<span class="channel-num">6</span>
<a href="http://c.biancheng.net/java/60/">Java内置包装类</a>
</dd>
<dd>
<span class="channel-num">7</span>
<a href="http://c.biancheng.net/java/70/">Java数组处理</a>
</dd>
<dd>
<span class="channel-num">8</span>
<a href="http://c.biancheng.net/java/80/">Java类和对象</a>
</dd>
<dd>
<span class="channel-num">9</span>
<a href="http://c.biancheng.net/java/90/">Java继承和多态</a>
</dd>
<dd>
<span class="channel-num">10</span>
<a href="http://c.biancheng.net/java/100/">Java异常处理</a>
</dd>
<dd>
<span class="channel-num">11</span>
<a href="http://c.biancheng.net/java/110/">Java集合、泛型和枚举</a>
</dd>
<dd>
<span class="channel-num">12</span>
<a href="http://c.biancheng.net/java/120/">Java反射机制</a>
</dd>
<dd class="this"> <span class="channel-num">13</span> <a href="http://c.biancheng.net/java/130/">Java输入/输出流</a> </dd><dl class="dl-sub"><dd>13.1 <a href="http://c.biancheng.net/view/1119.html">Java流的概念</a></dd><dd>13.2 <a href="http://c.biancheng.net/view/1129.html">Java系统流</a></dd><dd>13.3 <a href="http://c.biancheng.net/view/6977.html">Java字符编码介绍</a></dd><dd>13.4 <a href="http://c.biancheng.net/view/1133.html">Java File类</a></dd><dd>13.5 <a href="http://c.biancheng.net/view/vip_7085.html">Java动态读取文件内容</a><span class="glyphicon glyphicon-usd"></span></dd><dd>13.6 <a href="http://c.biancheng.net/view/1144.html">Java字节流的使用</a></dd><dd>13.7 <a href="http://c.biancheng.net/view/1150.html">Java字符流的使用</a></dd><dd>13.8 <a href="http://c.biancheng.net/view/vip_7086.html">Java字节流和字符流的区别,如何区分输入流和输出流?</a><span class="glyphicon glyphicon-usd"></span></dd><dd>13.9 <a href="http://c.biancheng.net/view/6966.html">Java转换流</a></dd><dd>13.10 <a href="http://c.biancheng.net/view/vip_7088.html">Java利用对象序列化控制输入输出</a><span class="glyphicon glyphicon-usd"></span></dd><dd>13.11 <a href="http://c.biancheng.net/view/1153.html">Java保存图书信息</a></dd><dd class="active">13.12 <a href="http://c.biancheng.net/view/vip_7089.html">【Java项目实战】猜数字小游戏</a><span class="glyphicon glyphicon-usd"></span></dd></dl>
<dd>
<span class="channel-num">14</span>
<a href="http://c.biancheng.net/java/java/135/">Java注解</a>
</dd>
</div>
</div>
<div id="article-wrap">
<div id="article">
<div class="arc-info">
<span class="position"><span class="glyphicon glyphicon-map-marker"></span> <a href="http://c.biancheng.net/">首页</a> > <a href="http://c.biancheng.net/java/">Java教程</a> > <a href="http://c.biancheng.net/java/130/">Java输入/输出流</a></span>
<span class="read-num">阅读:3,757</span>
</div>
<div id="ad-position-bottom"></div>
<h1>Java项目实战:猜数字小游戏</h1>
<div class="pre-next-page clearfix">
<span class="pre left"><span class="icon"><</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/1153.html">Java保存图书信息</a></span>
<span class="next right"><a href="http://c.biancheng.net/java/java/135/">Java注解</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">></span></span>
</div>
<div id="ad-arc-top"><p class="pic"></p><p class="text" adid="java-1v1q2a"><a href="http://c.biancheng.net/view/7552.html" target="_blank" style="color: #D33428;">C语言中文网推出辅导班啦,包括<span style="color: #008000;">「C语言辅导班、C++辅导班、算法/数据结构辅导班」</span>,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践 + 永久学习。QQ在线,随时响应!</a></p></div>
<div id="arc-body">猜数字是一个经典的小游戏,程序先产生一个随机数,然后用户输入数字,程序将输入的数字与随机数进行对比,给出用户相应的提示信息。<br>
<br>
本节实现了一个基于 IO 流的猜数字游戏,游戏中限制玩家游戏次数,游戏试玩为 5 次,超过 5 次后,则提示玩家试玩结束,请付费。具体实现步骤和代码如下:<br>
<br>
1)创建 count.txt 文件,存储游戏次数,文件内容如下:
<p class="info-box">
count=0</p>
2)创建 way.txt 文件,存储支付状态(1 为已付费,0 为未付费),文件内容如下:
<p class="info-box">
way=0</p>
3)为了简化代码,本节将多个实现方法写在同一个类中。创建 BullCows 类,代码如下:
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display: none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7089.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7089.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7089.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">BullCows</span> <span class="sh_cbracket">{</span></li><li> <span class="sh_comment">/**</span></li><li><span class="sh_comment"> * 负责调用对应的方法,实现整个案例的逻辑关系</span></li><li><span class="sh_comment"> *</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@param</span><span class="sh_comment"> args</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@throws</span><span class="sh_comment"> IOException</span></li><li><span class="sh_comment"> */</span></li><li> <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span>String<span class="sh_symbol">[]</span> args<span class="sh_symbol">)</span> <span class="sh_keyword">throws</span> IOException <span class="sh_cbracket">{</span></li><li> <span class="sh_keyword">while</span> <span class="sh_symbol">(</span><span class="sh_keyword">true</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li> <span class="sh_comment">// 获取游戏次数</span></li><li> <span class="sh_type">int</span> count <span class="sh_symbol">=</span> <span class="sh_function">getCount</span><span class="sh_symbol">();</span></li><li> <span class="sh_comment">// 获取付费状态</span></li><li> <span class="sh_type">boolean</span> flag <span class="sh_symbol">=</span> <span class="sh_function">getCondition</span><span class="sh_symbol">();</span></li><li> <span class="sh_comment">// 如果已付费,提示用户游戏次数解封可以继续游戏</span></li><li> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>flag<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"游戏已经付费,游戏次数已解封!"</span><span class="sh_symbol">);</span></li><li> <span class="sh_function">game</span><span class="sh_symbol">();</span></li><li> <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li> <span class="sh_comment">// 未付费且游戏次数超过5次时,提示试玩结束,要付费</span></li><li> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>count <span class="sh_symbol">>=</span> <span class="sh_number">5</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"试玩已经结束,请付费!"</span><span class="sh_symbol">);</span></li><li> <span class="sh_function">getMoney</span><span class="sh_symbol">();</span></li><li> <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li> <span class="sh_comment">// 未付费且游戏次数未超过5次时,继续游戏,游戏次数加1</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"----"</span> <span class="sh_symbol">+</span> <span class="sh_string">"试玩第"</span> <span class="sh_symbol">+</span> <span class="sh_symbol">(</span>count <span class="sh_symbol">+</span> <span class="sh_number">1</span><span class="sh_symbol">)</span> <span class="sh_symbol">+</span> <span class="sh_string">"次"</span> <span class="sh_symbol">+</span> <span class="sh_string">"----"</span><span class="sh_symbol">);</span></li><li> <span class="sh_function">game</span><span class="sh_symbol">();</span></li><li> <span class="sh_function">writeCount</span><span class="sh_symbol">();</span></li><li> <span class="sh_cbracket">}</span></li><li> <span class="sh_cbracket">}</span></li><li> <span class="sh_cbracket">}</span></li><li> <span class="sh_cbracket">}</span></li><li><span style="display:none;"> </span></li><li> <span class="sh_comment">/**</span></li><li><span class="sh_comment"> * 获取已经玩过的次数</span></li><li><span class="sh_comment"> *</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@return</span><span class="sh_comment"> temp count.txt文件中的游戏次数</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@throws</span><span class="sh_comment"> IOException</span></li><li><span class="sh_comment"> */</span></li><li> <span class="sh_keyword">private</span> <span class="sh_keyword">static</span> <span class="sh_type">int</span> <span class="sh_function">getCount</span><span class="sh_symbol">()</span> <span class="sh_keyword">throws</span> IOException <span class="sh_cbracket">{</span></li><li> <span class="sh_comment">// 创建Properties对象</span></li><li> <span class="sh_usertype">Properties</span><span class="sh_normal"> </span>prop <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Properties</span><span class="sh_symbol">();</span></li><li> <span class="sh_comment">// 使用FileReader对象获取count文件中的游戏次数</span></li><li> prop<span class="sh_symbol">.</span><span class="sh_function">load</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">FileReader</span><span class="sh_symbol">(</span><span class="sh_string">"count.txt"</span><span class="sh_symbol">));</span></li><li> <span class="sh_usertype">String</span><span class="sh_normal"> </span>property <span class="sh_symbol">=</span> prop<span class="sh_symbol">.</span><span class="sh_function">getProperty</span><span class="sh_symbol">(</span><span class="sh_string">"count"</span><span class="sh_symbol">);</span></li><li> <span class="sh_type">int</span> temp <span class="sh_symbol">=</span> Integer<span class="sh_symbol">.</span><span class="sh_function">parseInt</span><span class="sh_symbol">(</span>property<span class="sh_symbol">);</span></li><li> <span class="sh_keyword">return</span> temp<span class="sh_symbol">;</span></li><li> <span class="sh_cbracket">}</span></li><li><span style="display:none;"> </span></li><li> <span class="sh_comment">/**</span></li><li><span class="sh_comment"> * 支付方法,支付成功则把支付状态改为“1”并存到数据库,之后可以无限次玩游戏</span></li><li><span class="sh_comment"> *</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@throws</span><span class="sh_comment"> IOException</span></li><li><span class="sh_comment"> */</span></li><li> <span class="sh_keyword">private</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">getMoney</span><span class="sh_symbol">()</span> <span class="sh_keyword">throws</span> IOException <span class="sh_cbracket">{</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"请支付5元!"</span><span class="sh_symbol">);</span></li><li> <span class="sh_comment">// 获取键盘录入数据</span></li><li> <span class="sh_usertype">Scanner</span><span class="sh_normal"> </span>sc <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Scanner</span><span class="sh_symbol">(</span>System<span class="sh_symbol">.</span>in<span class="sh_symbol">);</span></li><li> <span class="sh_type">int</span> nextInt <span class="sh_symbol">=</span> sc<span class="sh_symbol">.</span><span class="sh_function">nextInt</span><span class="sh_symbol">();</span></li><li> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>nextInt <span class="sh_symbol">==</span> <span class="sh_number">5</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li> <span class="sh_comment">// 创建Properties对象</span></li><li> <span class="sh_usertype">Properties</span><span class="sh_normal"> </span>prop <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Properties</span><span class="sh_symbol">();</span></li><li> prop<span class="sh_symbol">.</span><span class="sh_function">setProperty</span><span class="sh_symbol">(</span><span class="sh_string">"way"</span><span class="sh_symbol">,</span> <span class="sh_string">"1"</span><span class="sh_symbol">);</span></li><li> <span class="sh_comment">// 使用FileWriter类将支付状态写入到way文件</span></li><li> prop<span class="sh_symbol">.</span><span class="sh_function">store</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">FileWriter</span><span class="sh_symbol">(</span><span class="sh_string">"way.txt"</span><span class="sh_symbol">),</span> <span class="sh_keyword">null</span><span class="sh_symbol">);</span></li><li> <span class="sh_cbracket">}</span></li><li> <span class="sh_cbracket">}</span></li><li><span style="display:none;"> </span></li><li> <span class="sh_comment">/**</span></li><li><span class="sh_comment"> * 将试玩的次数写入文档并保存</span></li><li><span class="sh_comment"> *</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@throws</span><span class="sh_comment"> IOException</span></li><li><span class="sh_comment"> */</span></li><li> <span class="sh_keyword">private</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">writeCount</span><span class="sh_symbol">()</span> <span class="sh_keyword">throws</span> IOException <span class="sh_cbracket">{</span></li><li> <span class="sh_comment">// 创建Properties对象</span></li><li> <span class="sh_usertype">Properties</span><span class="sh_normal"> </span>prop <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Properties</span><span class="sh_symbol">();</span></li><li> <span class="sh_type">int</span> count <span class="sh_symbol">=</span> <span class="sh_function">getCount</span><span class="sh_symbol">();</span></li><li> <span class="sh_comment">// 写入文件</span></li><li> prop<span class="sh_symbol">.</span><span class="sh_function">setProperty</span><span class="sh_symbol">(</span><span class="sh_string">"count"</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>count <span class="sh_symbol">+</span> <span class="sh_number">1</span><span class="sh_symbol">)</span> <span class="sh_symbol">+</span> <span class="sh_string">""</span><span class="sh_symbol">);</span></li><li> prop<span class="sh_symbol">.</span><span class="sh_function">store</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">FileWriter</span><span class="sh_symbol">(</span><span class="sh_string">"count.txt"</span><span class="sh_symbol">),</span> <span class="sh_keyword">null</span><span class="sh_symbol">);</span></li><li> <span class="sh_cbracket">}</span></li><li><span style="display:none;"> </span></li><li> <span class="sh_comment">/**</span></li><li><span class="sh_comment"> * 用来获取每次启动时的付费状态</span></li><li><span class="sh_comment"> *</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@return</span><span class="sh_comment"> flag 是否付费</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@throws</span><span class="sh_comment"> FileNotFoundException</span></li><li><span class="sh_comment"> * </span><span class="sh_type">@throws</span><span class="sh_comment"> IOException</span></li><li><span class="sh_comment"> */</span></li><li> <span class="sh_keyword">private</span> <span class="sh_keyword">static</span> <span class="sh_type">boolean</span> <span class="sh_function">getCondition</span><span class="sh_symbol">()</span> <span class="sh_keyword">throws</span> FileNotFoundException<span class="sh_symbol">,</span> IOException <span class="sh_cbracket">{</span></li><li> <span class="sh_type">boolean</span> flag <span class="sh_symbol">=</span> <span class="sh_keyword">false</span><span class="sh_symbol">;</span></li><li> <span class="sh_comment">// 创建Properties对象</span></li><li> <span class="sh_usertype">Properties</span><span class="sh_normal"> </span>prop <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Properties</span><span class="sh_symbol">();</span></li><li> <span class="sh_comment">// 读取way.txt文件,获取支付状态</span></li><li> prop<span class="sh_symbol">.</span><span class="sh_function">load</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">FileReader</span><span class="sh_symbol">(</span><span class="sh_string">"way.txt"</span><span class="sh_symbol">));</span></li><li> <span class="sh_usertype">String</span><span class="sh_normal"> </span>property <span class="sh_symbol">=</span> prop<span class="sh_symbol">.</span><span class="sh_function">getProperty</span><span class="sh_symbol">(</span><span class="sh_string">"way"</span><span class="sh_symbol">);</span></li><li> <span class="sh_type">int</span> parseInt <span class="sh_symbol">=</span> Integer<span class="sh_symbol">.</span><span class="sh_function">parseInt</span><span class="sh_symbol">(</span>property<span class="sh_symbol">);</span></li><li> <span class="sh_comment">// way的值等于1时,为已付费</span></li><li> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>parseInt <span class="sh_symbol">==</span> <span class="sh_number">1</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li> flag <span class="sh_symbol">=</span> <span class="sh_keyword">true</span><span class="sh_symbol">;</span></li><li> <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li> flag <span class="sh_symbol">=</span> <span class="sh_keyword">false</span><span class="sh_symbol">;</span></li><li> <span class="sh_cbracket">}</span></li><li> <span class="sh_keyword">return</span> flag<span class="sh_symbol">;</span></li><li> <span class="sh_cbracket">}</span></li><li><span style="display:none;"> </span></li><li> <span class="sh_comment">/**</span></li><li><span class="sh_comment"> * 实现游戏产生数字,获取玩家所猜数字等, 并对玩家每次输入,都会有相应的提示</span></li><li><span class="sh_comment"> */</span></li><li> <span class="sh_keyword">private</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">game</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span></li><li> <span class="sh_comment">// 产生随机数1~100</span></li><li> <span class="sh_type">int</span> random <span class="sh_symbol">=</span> <span class="sh_symbol">(</span><span class="sh_type">int</span><span class="sh_symbol">)</span> <span class="sh_symbol">(</span>Math<span class="sh_symbol">.</span><span class="sh_function">random</span><span class="sh_symbol">()</span> <span class="sh_symbol">*</span> <span class="sh_number">100</span> <span class="sh_symbol">+</span> <span class="sh_number">1</span><span class="sh_symbol">);</span></li><li> <span class="sh_comment">// 获取键盘录入数据</span></li><li> <span class="sh_usertype">Scanner</span><span class="sh_normal"> </span>sc <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Scanner</span><span class="sh_symbol">(</span>System<span class="sh_symbol">.</span>in<span class="sh_symbol">);</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"欢迎来到猜数字小游戏!"</span><span class="sh_symbol">);</span></li><li> <span class="sh_comment">// while循环进行游戏</span></li><li> <span class="sh_keyword">while</span> <span class="sh_symbol">(</span><span class="sh_keyword">true</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"请输入你猜的数据:"</span><span class="sh_symbol">);</span></li><li> <span class="sh_type">int</span> guess <span class="sh_symbol">=</span> sc<span class="sh_symbol">.</span><span class="sh_function">nextInt</span><span class="sh_symbol">();</span></li><li> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>guess <span class="sh_symbol">></span> random<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"大了"</span><span class="sh_symbol">);</span></li><li> <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>guess <span class="sh_symbol"><</span> random<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"小了"</span><span class="sh_symbol">);</span></li><li> <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li> System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"猜对了哦!"</span><span class="sh_symbol">);</span></li><li> <span class="sh_keyword">break</span><span class="sh_symbol">;</span></li><li> <span class="sh_cbracket">}</span></li><li> <span class="sh_cbracket">}</span></li><li> <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public class BullCows {
/**
* 负责调用对应的方法,实现整个案例的逻辑关系
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
while (true) {
// 获取游戏次数
int count = getCount();
// 获取付费状态
boolean flag = getCondition();
// 如果已付费,提示用户游戏次数解封可以继续游戏
if (flag) {
System.out.println("游戏已经付费,游戏次数已解封!");
game();
} else {
// 未付费且游戏次数超过5次时,提示试玩结束,要付费
if (count >= 5) {
System.out.println("试玩已经结束,请付费!");
getMoney();
} else {
// 未付费且游戏次数未超过5次时,继续游戏,游戏次数加1
System.out.println("----" + "试玩第" + (count + 1) + "次" + "----");
game();
writeCount();
}
}
}
}
/**
* 获取已经玩过的次数
*
* @return temp count.txt文件中的游戏次数
* @throws IOException
*/
private static int getCount() throws IOException {
// 创建Properties对象
Properties prop = new Properties();
// 使用FileReader对象获取count文件中的游戏次数
prop.load(new FileReader("count.txt"));
String property = prop.getProperty("count");
int temp = Integer.parseInt(property);
return temp;
}
/**
* 支付方法,支付成功则把支付状态改为“1”并存到数据库,之后可以无限次玩游戏
*
* @throws IOException
*/
private static void getMoney() throws IOException {
System.out.println("请支付5元!");
// 获取键盘录入数据
Scanner sc = new Scanner(System.in);
int nextInt = sc.nextInt();
if (nextInt == 5) {
// 创建Properties对象
Properties prop = new Properties();
prop.setProperty("way", "1");
// 使用FileWriter类将支付状态写入到way文件
prop.store(new FileWriter("way.txt"), null);
}
}
/**
* 将试玩的次数写入文档并保存
*
* @throws IOException
*/
private static void writeCount() throws IOException {
// 创建Properties对象
Properties prop = new Properties();
int count = getCount();
// 写入文件
prop.setProperty("count", (count + 1) + "");
prop.store(new FileWriter("count.txt"), null);
}
/**
* 用来获取每次启动时的付费状态
*
* @return flag 是否付费
* @throws FileNotFoundException
* @throws IOException
*/
private static boolean getCondition() throws FileNotFoundException, IOException {
boolean flag = false;
// 创建Properties对象
Properties prop = new Properties();
// 读取way.txt文件,获取支付状态
prop.load(new FileReader("way.txt"));
String property = prop.getProperty("way");
int parseInt = Integer.parseInt(property);
// way的值等于1时,为已付费
if (parseInt == 1) {
flag = true;
} else {
flag = false;
}
return flag;
}
/**
* 实现游戏产生数字,获取玩家所猜数字等, 并对玩家每次输入,都会有相应的提示
*/
private static void game() {
// 产生随机数1~100
int random = (int) (Math.random() * 100 + 1);
// 获取键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到猜数字小游戏!");
// while循环进行游戏
while (true) {
System.out.println("请输入你猜的数据:");
int guess = sc.nextInt();
if (guess > random) {
System.out.println("大了");
} else if (guess < random) {
System.out.println("小了");
} else {
System.out.println("猜对了哦!");
break;
}
}
}
}</pre></div></div>
第一次运行时,结果如下:
<p class="info-box">
----试玩第1次----<br>
欢迎来到猜数字小游戏!<br>
请输入你猜的数据:<br>
1<br>
小了<br>
请输入你猜的数据:<br>
5<br>
小了<br>
请输入你猜的数据:<br>
8<br>
小了<br>
请输入你猜的数据:<br>
9<br>
小了<br>
请输入你猜的数据:<br>
10<br>
猜对了哦!</p>
此时可以看到 count.txt 文件中 count 的值为 1。当进行 5 次游戏后,运行结果如下:
<p class="info-box">
试玩已经结束,请付费!<br>
请支付5元!<br>
5<br>
游戏已经付费,游戏次数已解封!<br>
欢迎来到猜数字小游戏!<br>
请输入你猜的数据:</p>
此时 count.txt 文件中 count 的值为 5,way.txt 文件中 way 的值为 1。<br>
<br>
<div>
示例中用到 Properties 类的几个方法,方法说明如下:</div>
<ol>
<li>
getProperty (String key):用指定的键在此属性列表中搜索属性。也就是通过参数 key,得到 key 所对应的 value。</li>
<li>
load (InputStream inStream):从输入流中读取属性列表(键和元素对)。通过对指定的文件进行装载来获取该文件中的所有键值对。以供 getProperty (String key) 来搜索。</li>
<li>
setProperty (String key, String value) :调用 Hashtable 的方法 put,通过调用基类的 put 方法来设置键值对。</li>
<li>
store (OutputStream out, String comments):与 load 方法相反,该方法是将键值对写入到指定的文件中。</li>
</ol>
</div>
<div id="arc-append">
<p>关注微信公众号「站长严长生」,在手机上阅读所有教程,随时随地都能学习。本公众号由<a class="col-link" href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">C语言中文网站长</a>运营,每日更新,坚持原创,敢说真话,凡事有态度。</p>
<p style="margin-top:12px; text-align:center;">
<img width="180" src="./13.12Java项目实战:猜数字小游戏_files/qrcode_weixueyuan_original.png" alt="魏雪原二维码"><br>
<span class="col-green">微信扫描二维码关注公众号</span>
</p>
</div>
<!-- <p>关注微信公众号「魏雪原」,跟着<a class="col-link" href="/view/8092.html" target="_blank">站长</a>一起学习,拒绝躺平,跳出内卷,用知识完成阶级跃升:</p>
<ul>
<li>每日更新,坚持原创,敢说真话,凡事有态度。</li>
<li>编程+求职+创业+营销,既有硬知识,也有软技能。</li>
<li>手机端阅读教程,随时随地都能学习。</li>
<li>即将绑定网站部分功能,包括注册登录、找回密码、获取下载链接等。</li>
</ul> -->
<div class="pre-next-page clearfix">
<span class="pre left"><span class="icon"><</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/1153.html">Java保存图书信息</a></span>
<span class="next right"><a href="http://c.biancheng.net/java/java/135/">Java注解</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">></span></span>
</div>
<div id="ad-arc-bottom"></div>
<!-- <div id="ad-bottom-weixin" class="clearfix">
<div class="left" style="width: 535px;">
<p><span class="col-red">编程帮</span>,一个分享编程知识的公众号。跟着<a class="col-link" href="/cpp/about/author/" target="_blank">站长</a>一起学习,每天都有进步。</p>
<p>通俗易懂,深入浅出,一篇文章只讲一个知识点。</p>
<p>文章不深奥,不需要钻研,在公交、在地铁、在厕所都可以阅读,随时随地涨姿势。</p>
<p>文章不涉及代码,不烧脑细胞,人人都可以学习。</p>
<p>当你决定关注「编程帮」,你已然超越了90%的程序员!</p>
</div>
<div class="right" style="width: 150px;">
<img width="150" src="/templets/new/images/erweima_biancheng.gif?v=1.6.72" alt="编程帮二维码" /><br />
<span class="col-green">微信扫描二维码关注</span>
</div>
</div> -->
<div id="nice-arcs" class="box-bottom">
<h4>优秀文章</h4>
<ul class="clearfix">
<li><a href="http://c.biancheng.net/view/958.html" title="Shell(Bash)用户自定义变量使用方法详解">Shell(Bash)用户自定义变量使用方法详解</a></li>
<li><a href="http://c.biancheng.net/view/1221.html" title="Java Swing JTextArea:文本域组件">Java Swing JTextArea:文本域组件</a></li>
<li><a href="http://c.biancheng.net/view/1552.html" title="JSP EL逻辑运算符">JSP EL逻辑运算符</a></li>
<li><a href="http://c.biancheng.net/view/1953.html" title="SOM自组织映射法原理及(TensorFlow)实现详解">SOM自组织映射法原理及(TensorFlow)实现详解</a></li>
<li><a href="http://c.biancheng.net/view/2594.html" title="MySQL修改存储过程(ALTER PROCEDURE)">MySQL修改存储过程(ALTER PROCEDURE)</a></li>
<li><a href="http://c.biancheng.net/view/2820.html" title="C#方法的参数(实参和形参)">C#方法的参数(实参和形参)</a></li>
<li><a href="http://c.biancheng.net/view/3624.html" title="汇编语言堆栈帧简介">汇编语言堆栈帧简介</a></li>
<li><a href="http://c.biancheng.net/view/6957.html" title="SEO关键词有什么研究的价值?">SEO关键词有什么研究的价值?</a></li>
<li><a href="http://c.biancheng.net/view/vip_8076.html" title="如何实现C++和C的混合编程?">如何实现C++和C的混合编程?</a></li>
<li><a href="http://c.biancheng.net/view/8271.html" title="IPython与Jupyter Notebook以及Anaconda">IPython与Jupyter Notebook以及Anaconda</a></li>
</ul>
</div>
</div>
</div>
</div>
<script type="text/javascript">
// 当前文章ID
window.arcIdRaw = 'a_' + 7089;
window.arcId = "0a55Cfk+PQC8/hECTu6Zddv7e+j+4pgqxpauSUcSVqeeFZiwrA2RfGaMLiU";
window.typeidChain = "109|79";
</script>
<div id="footer" class="clearfix">
<div class="info left">
<p>精美而实用的网站,分享优质编程教程,帮助有志青年。千锤百炼,只为大作;精益求精,处处斟酌;这种教程,看一眼就倾心。</p>
<p>
<a href="http://c.biancheng.net/view/8066.html" target="_blank" rel="nofollow">关于网站</a> <span>|</span>
<a href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">关于站长</a> <span>|</span>
<a href="http://c.biancheng.net/view/8097.html" target="_blank" rel="nofollow">如何完成一部教程</a> <span>|</span>
<a href="http://c.biancheng.net/view/8093.html" target="_blank" rel="nofollow">联系我们</a> <span>|</span>
<a href="http://c.biancheng.net/sitemap/" target="_blank" rel="nofollow">网站地图</a>
</p>
<p>Copyright ©2012-2021 biancheng.net, <a href="http://www.beian.miit.gov.cn/" target="_blank" rel="nofollow" style="color:#666;">陕ICP备15000209号</a></p>
</div>
<img class="right" src="./13.12Java项目实战:猜数字小游戏_files/logo_bottom.gif" alt="底部Logo">
<span id="return-top"><b>↑</b></span>
</div>
<script type="text/javascript">
window.siteId = 4;
window.cmsTemplets = "/templets/new";
window.cmsTempletsVer = "1.6.72";
</script>
<script src="./13.12Java项目实战:猜数字小游戏_files/jquery1.12.4.min.js.下载"></script>
<script src="./13.12Java项目实战:猜数字小游戏_files/common.js.下载"></script>
<span style="display:none;"><script src="./13.12Java项目实战:猜数字小游戏_files/z_stat.php" type="text/javascript" defer="defer" async="async"></script></span>
</body></html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。