您的位置:首页 > 编程语言 > Java开发

java案例-基于IO流的猜数字小游戏

2017-05-18 08:49 483 查看
java案例-基于IO流的猜数字小游戏

1.需求分析:

实现基于IO流的猜数字小游戏,游戏试玩为5次,超过5次,提示试玩结束,请付费。

2.实现分析:

(1)基于游戏开始有5次试玩机会,超过5次,试玩结束的大背景实现此案例。为了简化代码,此案例只在一个类中实现,写了几个方法分别实现各个功能

(2)每次游戏开始,首先会调用getCondition()方法,来获取付费的状态:

如果已经付费,则提示已经付费,游戏次数解封

如果未付费,再判断已经试玩的次数

(3)调用geyCount()方法,来获取已经试玩的次数:

如果次数大于等于5,则提示试玩结束,请付费;

如果次数小于5,则可以正常进行游戏

(4)每次玩完游戏,在游戏计数文档(prop.txt)中,对应的次数加一

(5)一旦付费成功,在付费记录文档(way.txt)进行状态存储

3.代码详解:

(1)main()方法:

分析:使用while循环,可以让游戏可以一直玩下去,这里的main()方法主要就是起到一个提示的作用,以及调用对应的方法,实现整个案例的逻辑关系

public static void main(String[] args) throws IOException {

while(true){
//获取次数
int count = getCount();
//获取已经付费状态
boolean flag = getCondition();

if(flag){
System.out.println("游戏已经付费,玩耍次数解封!");
game();
}else{
if(count>=5){
System.out.println("试玩已经结束,请付费!");
getmoney();
}else{
System.out.println("----"+"试玩第"+(count+1)+"次"+"----");
game();
writecount();
}
}
}

}


c38e

(2)getCount()方法:

分析:实现简单,就是获取已经玩过的次数,把值传给main()方法

//获取已经玩过的次数
private static int getCount() throws IOException {
//创建Properties对象
Properties prop = new Properties();

//获取次数
prop.load(new FileReader("prop.txt"));

String property = prop.getProperty("count");

int temp = Integer.parseInt(property);

return temp;
}


(3)getMoney()方法:

分析:此案例没有实现支付接口,这里只是简单写个支付的方法,用来表示支付的过程。支付成功,则将会把已经支付的状态“1”写入文档存储,之后可以无限次玩耍游戏

//支付的方法
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");
prop.store(new FileWriter("way.txt"), null);
}
}


(4)writeCount()方法:

分析:该方法用来将试玩的次数写入文档中进行保存

//写入试玩的次数
private static void writeCount() throws IOException {
//创建Properties对象
Properties prop = new Properties();

int count = getCount();

//写入文件
prop.setProperty("count", (count+1)+"");

prop.store(new FileWriter("prop.txt"),null);
}
(5)getCondition()方法:

分析:该方法用来每次启动游戏获取付费的状态,实现也比较简单

//获取付费状态
private static boolean getCondition() throws FileNotFoundException, IOException {
boolean flag = false;
//创建Properties对象
Properties prop = new Properties();
//获取是否已经付费状态
prop.load(new FileReader("way.txt"));
String property = prop.getProperty("way");
int parseInt = Integer.parseInt(property);
if(parseInt==1){
flag = true;
}else{
flag = false;
}
return flag;
}


(6)game()方法:

分析:该游戏的核心代码,具体实现了游戏,产生随机数、获取玩家猜的数字等,实现并不困难,while()循环控制游戏一直到猜对才结束,对于玩家的每次输入,都会给出对应的提示

private static void game() {
//产生随机数
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;
}
}

}


4.运行截图:

(1)开始运行:



(2)一次游戏部分:



(3)prop.txt文档数值已经变化:



(4)当游戏玩过5次:





(5)模拟支付:



(6)付费状态存储发生变化:



(7)再次运行程序:



5.案例总结:

利用IO流,实现了一个简单的猜数字小游戏,并且把游戏的玩耍状态存储了下来。难度并不大,主要就是逻辑关系的训练了,什么时候干什么,调用哪个方法实现,这点比较重要。还有就是各种方法的封装,把各个功能封装成一个个的方法,方便调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐