黑马程序员技术交流社区

标题: Properties练习中的小问题,请教. [打印本页]

作者: 乔玉吉    时间: 2012-3-24 01:11
标题: Properties练习中的小问题,请教.
//用于记录应用程序运行次数的小程序.如果运行次数超过5次将会提示注册.
import java.io.*;
import java.util.*;
class  RunCount
{
        public static void main(String[] args) throws IOException
        {
                Properties prop =  new Properties();

                File file = new File("count.ini");
                if(!file.exists())
                        file.createNewFile();

                FileInputStream fis = new FileInputStream(file);

                prop.load(fis);
               
                int count = 0;
                String value = prop.getProperty("time");

                if(value!=null)
                {
                        count = Integer.parseInt(value);
                        if(count>=5)
                        {
                                System.out.println("您好,使用次数已到,请注册");
                        }
                        return ;
                }

                count++;

                prop.setProperty("time",count+"");

                FileOutputStream fos = new FileOutputStream(file);

                prop.store(fos,"");
               
                fos.close();
                fis.close();
        }
}


配置文件中的time对应的值一直不改变(关了重新打开后还是不改变) 不知道代码哪里出错了 .

#
#Sat Mar 24 00:45:01 CST 2012
time=1

作者: 乔玉吉    时间: 2012-3-24 01:17
不好意思,我发现问题所在了, 是ruturn 没写到if语句中
作者: 陈从宾    时间: 2012-3-24 07:38
return语句具有结束函数执行的作用。您的代码return语句应该放在 System.out.println("您好,使用次数已到,请注册");语句后,
如下:
if(value!=null)
                {
                        count = Integer.parseInt(value);
                        if(count>=5)
                        {
                                System.out.println("您好,使用次数已到,请注册");
                                 return ;
                        }
                       
                }




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