写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
/*
* 我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
*/
public class PropertiesTest2 {
publicstatic void main(String[] args) throws IOException {
//读取某个地方的数据,如果次数不大于5,可以继续玩。否则就提示"游戏试玩已结束,请付费。"
//创建一个文件
//File file = new File("count.txt");
//if (!file.exists()) {
//file.createNewFile();
//}
//把数据加载到集合中
Propertiesprop = new Properties();
Readerr = new FileReader("count.txt");
prop.load(r);
r.close();
//我自己的程序,我当然知道里面的键是谁
Stringvalue = prop.getProperty("count");
intnumber = Integer.parseInt(value);
if(number > 5) {
System.out.println("游戏试玩已结束,请付费。");
System.exit(0);
}else {
number++;
prop.setProperty("count",String.valueOf(number));
Writerw = new FileWriter("count.txt");
prop.store(w,null);
w.close();
GuessNumber.start();
}
}
}

本文介绍了一个使用Java实现的游戏试玩次数限制程序。通过读取和更新本地文件中的计数器来控制玩家的试玩次数,确保不超过五次免费体验的机会。
662

被折叠的 条评论
为什么被折叠?



