黑马程序员技术交流社区

标题: 代码那个地方错啦 [打印本页]

作者: 途中ms前进    时间: 2015-5-23 17:37
标题: 代码那个地方错啦
本帖最后由 途中ms前进 于 2015-5-25 21:08 编辑

记录程序运行次数,如果使用次数已到那么给出注册提示;

import java.io.*;
import java.util.*;
class PropertiesTimeDemo
{
        public static void main(String[] args) throws IOException
        {File file=new File("count.properties");
        if(!file.exists())
                file.createNewFile();
                Properties pro=new Properties();
                FileInputStream fos=new FileInputStream(file);
                pro.load(fos);
                int count=0;
                String value=pro.getProperty("time");
                        if(value!=null)
                        count=Integer.parseInt(value);
                if(count>=5)
                {
                System.out.println("次数已到请注册交钱哈哈哈哈");
                }
               
                pro.setProperty("time",count+"");
                FileOutputStream fis=new FileOutputStream(file);
                pro.store(fis,"");
                fis.close();
                fos.close();
               

        }
}
//怎么不提示交钱
作者: pvbj0314    时间: 2015-5-23 21:13
将代码修改如下,可实现你的需求:
  1. import java.io.*;
  2. import java.util.*;
  3. public class PropertiesTimeDemo
  4. {
  5.         public static void main(String[] args) throws IOException
  6.         {
  7.             File file=new File("count.properties");
  8.             if(!file.exists())
  9.                 file.createNewFile();
  10.             Properties pro=new Properties();
  11.             FileInputStream fos=new FileInputStream(file);
  12.             pro.load(fos);
  13.             int count;
  14.             String value=pro.getProperty("time");
  15.             //System.out.println("pro.getProperty(\"time\") = " + value);
  16.             if(value!=null)
  17.             {
  18.                 count=Integer.parseInt(value);
  19.                 if(count>=5)
  20.                 {
  21.                     System.out.println("次数已到请注册交钱哈哈哈哈");
  22.                 }
  23.                 pro.setProperty("time",(++count)+"");
  24.             }
  25.             FileOutputStream fis=new FileOutputStream(file);
  26.             pro.store(fis,"");
  27.             fis.close();
  28.             fos.close();
  29.                

  30.         }
  31. }
复制代码








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2