• Properties集合的练习


    1、更改文件中的数据,特定键的值:

    需求:我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。
       请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100”
    分析:
      A:把文本文件中的数据加载到Properties集合中
      B:获取该集合的键集合
      C:遍历键集合,进行判断是否是lisi键
              是:更改为“100”
              不是:不理
      D:把更改后的集合数据存储到文本中

     1 public class PropertiesTest1 {
     2 
     3     public static void main(String[] args) throws IOException {
     4             
     5         
     6         //创建集合
     7         Properties prop = new Properties();
     8         Reader r = new FileReader("user.txt");
     9         //public void load(Reader reader):把文件中的数据读取到集合中
    10         prop.load(r);
    11         r.close();
    12         
    13         //遍历集合,获取键集合
    14         //public Set<String> stringPropertyNames()返回此属性列表中的键集,
    15         //其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键
    16         Set<String> s = prop.stringPropertyNames();
    17         for(String key : s){
    18             if(prop.get(key).equals("lisi")){
    19                 prop.setProperty(key, "100");
    20             }
    21         }
    22         
    23         //把集合中的数据存储到文本中
    24         Writer w = new FileWriter("user.txt");
    25         prop.store(w,"更改后");
    26         w.close();
    27         
    28     }
    29 
    30 }

    2、一些游戏,都有试玩次数或者关卡,当玩到特定的次数或者关卡时,就要求付费。。。来试试如何实现:

    下面是次数版本,关卡差不多。

    需求:

        我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
    分析:
        A:创建一个文本文件,里面写一段键值对数据:count=0(不需要程序创建)
        B:创建properties集合,把文本中的数据加载到集合中
        C:获取count键的值
        D:判断次数
            超过5次:提示
            不超过:count键的值+1,并把这段数据重新存储到文本文件中,继续游戏

     1 public class PropertiesTest2 {
     2 
     3     public static void main(String[] args) throws IOException {
     4             
     5         //创建集合
     6         Properties prop = new Properties();
     7         //把文本的数据加载到集合中
     8         Reader r = new FileReader("count.txt");
     9         prop.load(r);
    10         r.close();
    11         
    12         //获取count键的值
    13         String value = prop.getProperty("count");
    14         //把字符串的值转换成int,方便判断
    15         int number = Integer.parseInt(value);
    16         
    17         //对number进行判断
    18         if(number > 5){
    19             System.out.println("试玩次数已到,请付费");
    20             System.exit(0);
    21         }
    22         else{
    23             number ++;
    24             //重新把数据存储到文本中
    25             
    26             //number必须转成字符串
    27             prop.setProperty("count", String.valueOf(number));        
    28             Writer w = new FileWriter("count.txt");
    29             //存储
    30             prop.store(w, null);
    31             w.close();
    32             //继续
    33             GuessGame.start();
    34         }
    35 
    36     }
    37 
    38 }

    。。。产生随机数并猜测判断的类,不写了

    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    nsurlSession
    IOS 锁
    常见问题
    xcode编译太慢
    ios之keychain
    iOS多线程系列(四)----GCD之Dispatch Queues
    iOS多线程系列(三)----NSOperationQueue
    iOS多线程系列(二)------NSOperation
    iOS多线程系列(一)------NSThread
    kvm虚拟机最佳实践系列3-kvm克隆和静态迁移
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5931894.html
Copyright © 2020-2023  润新知