我怎么能从块外部访问变量 [英] how could i access variable from outside of a block

查看:75
本文介绍了我怎么能从块外部访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(int i=0,i<=5;i++)
{
int cnt=0;
cnt++;
}
response.write(cnt);   //here cnt is inaccessable

推荐答案

更改

change
for(int i=0,i<=5;i++)
{
int cnt=0;
cnt++;
}
response.write(cnt);



to


to

int cnt=0;
for(int i=0,i<=5;i++)
{
cnt++;
}
response.write(cnt);



基于你到目前为止所说的内容。


based on what you have said so far.


理想情况下,你不应该在for循环中声明变量。



你应该总是在循环之外声明它。否则变量将被创建多次,这是不必要的。
Ideally you should not declare the variable inside the for loop.

You should always declare it outside the loop. Otherwise the variable will get created many times, which is not necessary.


int cnt=0;
for(int i=0,i<=5;i++)
{
cnt++;
}
response.write(cnt);


这篇关于我怎么能从块外部访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆