黑马程序员技术交流社区

标题: 这句放到头部会让count++不起效,为什么? [打印本页]

作者: 何清林    时间: 2014-2-27 17:02
标题: 这句放到头部会让count++不起效,为什么?
public class PropertiesTest {
    public static void main(String[] args) throws IOException {
                BufferedReader bufr = null;
                BufferedWriter bufw = null;
                bufr = new BufferedReader(new FileReader("D:\\workspace\\exam\\src\\com\\itheima\\Review\\Prop.txt"));
                Properties prop = new Properties();

                prop.load(bufr); //放到这句前面count++不起效,后面就可以起效,不明白
                int count=0;
                String value = prop.getProperty("time");
                if(value!=null){
                        count = Integer.parseInt(value);
                        if(count==5){
                                System.out.println("您好,使用次数已到,拿钱!");
                                return ;
                        }
                }
                count++;
                //这句放到头部会让count++不起效,为什么?
                bufw = new BufferedWriter(new FileWriter("D:\\workspace\\exam\\src\\com\\itheima\\Review\\Prop.txt"));
                prop.setProperty("time", count+"");
                prop.store(bufw,"");

                Set set = prop.keySet();
                Iterator it = set.iterator();
                while(it.hasNext()){               
                        Object key = it.next();
                        Object value1 = prop.get(key);
                        System.out.println(key+"::"+value1);
                }

                bufr.close();
                bufw.close();
        }
}


作者: 平凡成就非凡    时间: 2014-2-27 17:08
你放到这句int count=0;前面怎么会有效啊
作者: flying    时间: 2014-2-27 20:06
  1.          if(value!=null){
  2.                         count = Integer.parseInt(value);
  3.                         if(count==5){
  4.                                 System.out.println("您好,使用次数已到,拿钱!");
  5.                                 return ;
  6.                         }
  7.                 }
  8.                 count++;
复制代码

这句话的意思是得到的次数大于等于5就告诉他使用次数已经到了
如果没有大于5则count++    你现在把count放在前面 等于 每次从prop.txt
种读取数据  然后原样写入 当然就没效果了





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