Java 如何在每次运行方法时进行变量增量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24570201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a variable increment on every running of a method?
提问by user3804738
I am trying to get the int count to increment each time I run the program. ie: So if I ran the program 9 times, and doMethod was called 9 times, the value of count would be 9. But since I have to initialize count to = 0 count keeps resetting itself to 0 on every iteration of the method. Is there a way around this?
每次运行程序时,我都试图让 int 计数增加。即:因此,如果我运行该程序 9 次,并且 doMethod 被调用 9 次,则 count 的值将是 9。但是由于我必须将 count 初始化为 = 0 count 在该方法的每次迭代中不断将自身重置为 0。有没有解决的办法?
public class Test {
public static void main (String[] args) {
Test test1 = new Test();
test1.doMethod();
}
public void doMethod () {
int count = 0;
count++;
System.out.println(count);
}
}
回答by TheLostMind
declare int count = 0;
either at instancelevel or at classlevel.
声明int count = 0;
无论是在实例级或类级别。
public void doMethod() {
int count = 0; // a new count variable is created each time doMethod() is called
count++;
System.out.println(count);
}
回答by Suresh Atta
Instead of making it as a local to method, make it as instance member.
与其将其作为本地方法,不如将其作为实例成员。
int count = 0;
-----
public void doMethod() {
count++;
System.out.println(count);
}
So that it wont reset to 0
on each call of doMethod()
.
这样它就不会0
在每次调用doMethod()
.
回答by Simon
If you declare the variable outside of a method, the state is remembered.
如果在方法之外声明变量,状态会被记住。
A solution could be:
一个解决方案可能是:
public class Test {
int count = 0;
public static void main(String[] args) {
Test test1 = new Test();
test1.doMethod();
test1.doMethod();
test1.doMethod();
}
public void doMethod () {
count++;
System.out.println(count);
}
}
This way count
is created the moment you call new Test()
and will be remembered until the Object is destroyed. Variables have something called a 'scope'. They can only be accessed in their scope and will only exist in that scope. Because you created count
inside your void doMethod()
method, it did not exist anywhere else. An easy way to look at the scope is by watching the brackets { }
your variables are only stored inside those brackets. In my solution the brackets for count
are the brackets for the entire Test
class. Thus the variable is stored until the Object is destroyed.
这种方式count
是在您调用的那一刻创建的,new Test()
并且会被记住,直到对象被销毁。变量有一个叫做“作用域”的东西。它们只能在其作用域内访问,并且只会存在于该作用域中。因为您是count
在void doMethod()
方法内部创建的,所以它在其他任何地方都不存在。查看范围的一种简单方法是查看括号,{ }
您的变量仅存储在这些括号内。在我的解决方案中,括号 forcount
是整个Test
班级的括号。因此,变量会一直存储到对象被销毁为止。
More on scope: http://en.wikipedia.org/wiki/Scope_(computer_science)
更多关于范围:http: //en.wikipedia.org/wiki/Scope_(computer_science)
I just noticed you mentioned you want the count
value to remain after you run the program. You can do this by saving it in a database or file. However, you might have meant that you want the count
value to remain after you run the void doMethod()
method. I have edited my solution to execute the void doMethod()
method three times so you see the value actually remains after running the method.
我刚刚注意到您提到您希望在count
运行程序后保留该值。您可以通过将其保存在数据库或文件中来做到这一点。但是,您可能表示希望在count
运行该void doMethod()
方法后保留该值。我已经编辑了我的解决方案以执行该void doMethod()
方法三次,因此您会看到运行该方法后实际保留的值。
回答by Jaithera
If you want to increment count each time you run the program,
如果你想在每次运行程序时增加计数,
- You have to store your
counter
variable count into a file or a database table - So every time when execution starts get value from storage-initialize to count-increment and print in methodand store to storage after completion of method.
- If you do so the variable count will be initialized to new value for each run of the program.
- 您必须将
counter
变量计数存储到文件或数据库表中 - 所以,每次当执行开始时间从存储获得价值-初始化计数-增量和方法打印和方法完成后,店里的存储。
- 如果你这样做,变量计数将被初始化为每次程序运行的新值。
回答by Ashvini Marwal
Define your count variable out of that method:
使用该方法定义您的计数变量:
public class Test {
int count = 0;
public static void main(String[] args) {
Test test1 = new Test();
test1.doMethod();
}
public void doMethod() {
count++;
System.out.println(count);
}
}
回答by gkbstar
As you want to know how many number of times your program has executed including current execution. So, for this either you need to write count to a file or you need to create a registry where you can put your counter and increase all the time your program execute through your program:
因为您想知道您的程序执行了多少次,包括当前执行。因此,为此,您需要将计数写入文件,或者您需要创建一个注册表,您可以在其中放置计数器并增加程序通过程序执行的所有时间:
Following is an example of storing execution counter to a text file.
以下是将执行计数器存储到文本文件的示例。
class Test {
public static void main(String[] args) {
Test test1 = new Test();
test1.doMethod();
}
public int getCount() {
int count = 0;
try {
if ( !new File("d:\myCount.txt").exists())
return 1;
else {
BufferedReader br = new BufferedReader(new FileReader(new File("d:\myCount.txt")));
String s = br.readLine();
count = Integer.parseInt(s);
br.close();
}
} catch(Exception e) {
e.printStackTrace();
}
return count;
}
public void putCount(int count) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("d:\myCount.txt")));
bw.write(Integer.toString(count));
bw.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public void doMethod() {
int count = getCount();
System.out.println("You are running this program " + count + " number of times");
count++;
putCount(count);
}
}
回答by Ramesh Hiremath
public class Arguments {
private static int i = 1;
// Arguments() {
// System.out.println("Main method thread constructor incremented for "+ i++ + " time" +"\n");
// }
public static void main(String[] args) {
new Arguments().method();
new Arguments().method();
new Arguments().method();
new Arguments().method();
new Arguments().method();
new Arguments().method();
}
private void method() {
System.out.println("Main method value incremented for "+ i++ + " time" +"\n");
}
}
回答by Oghli
you can declare static
variable count
你可以声明static
变量count
count
will be part of the class, not part of any individual object.
count
将是类的一部分,而不是任何单个对象的一部分。
now you only need to increment a count variable in each method call:
现在你只需要在每个方法调用中增加一个计数变量:
public class Test {
// Set count to zero initially.
static int count = 0;
public static void main (String[] args) {
Test test1 = new Test();
test1.doMethod();
}
public void doMethod () {
// Every time the method calls, increment count.
count++;
System.out.println(count);
}
}
Update:
更新:
also you can instead of printing count inside doMethod ()
after each call
您也可以doMethod ()
在每次通话后在 内部打印计数而不是
you can define static
method as part of the class to get the value of the static variable count
after method calls:
您可以将static
方法定义为类的一部分,以count
在方法调用后获取静态变量的值:
class Test
{
// Set count to zero initially.
static int count = 0;
public void doMethod () {
// Every time the method calls, increment count.
count++;
}
static int getCount(){
// get latest value of count after method calls
return count;
}
public static void main (String[] args) throws java.lang.Exception
{
Test test1 = new Test();
test1.doMethod();
test1.doMethod();
test1.doMethod();
System.out.println(Test.getCount());
}
}
回答by Suyash Saurabh
public static int count;
public int get(){
count=count+1;
return count;
}