这位怪蜀黍 快来逗我玩吧!

关闭页面特效

猜数字是一个经典的小游戏,程序先产生一个随机数,然后用户输入数字,程序将输入的数字与随机数进行对比,给出用户相应的提示信息。

本节实现了一个基于 IO 流的猜数字游戏,游戏中限制玩家游戏次数,游戏试玩为 5 次,超过 5 次后,则提示玩家试玩结束,请付费。具体实现步骤和代码如下:

1)创建 count.txt 文件,存储游戏次数,文件内容如下:

count=0

2)创建 way.txt 文件,存储支付状态(1 为已付费,0 为未付费),文件内容如下:

way=0

3)为了简化代码,本节将多个实现方法写在同一个类中。创建 BullCows 类,代码如下:

复制代码
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;
}
}
}
}
复制代码

 

第一次运行时,结果如下:

复制代码
----试玩第1次----
欢迎来到猜数字小游戏!
请输入你猜的数据:
1
小了
请输入你猜的数据:
5
小了
请输入你猜的数据:
8
小了
请输入你猜的数据:
9
小了
请输入你猜的数据:
10
猜对了哦!
复制代码

 

此时可以看到 count.txt 文件中 count 的值为 1。当进行 5 次游戏后,运行结果如下:

试玩已经结束,请付费!
请支付5元!
5
游戏已经付费,游戏次数已解封!
欢迎来到猜数字小游戏!
请输入你猜的数据:

此时 count.txt 文件中 count 的值为 5,way.txt 文件中 way 的值为 1。

示例中用到 Properties 类的几个方法,方法说明如下:
    1. getProperty (String key):用指定的键在此属性列表中搜索属性。也就是通过参数 key,得到 key 所对应的 value。
    2. load (InputStream inStream):从输入流中读取属性列表(键和元素对)。通过对指定的文件进行装载来获取该文件中的所有键值对。以供 getProperty (String key) 来搜索。
    3. setProperty (String key, String value) :调用 Hashtable 的方法 put,通过调用基类的 put 方法来设置键值对。
    4. store (OutputStream out, String comments):与 load 方法相反,该方法是将键值对写入到指定的文件中。
 posted on   大码王  阅读(719)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

成都

复制代码

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示