Accesing a int value from a method into another method?
I have a method, which generates a random number, heres the code for it:
public int generateNumber(){
int randomnum = generator.nextInt(5);
System.out.println(randomnum);
return randomnum;
}
Now, I want to acces the randomnum int value, into anothe开发者_运维知识库r method, in the same class file, in an if-statement. Like this:
public DrawPanel(){
timer.schedule(new LeTimer(), 0, 1*1000);
if(randomnum == 3){
System.out.println("3 has been counted.");
}
}
How would I do this?
declare the int in class scope then all methods can access it
class
{
<class scope variables>
method1() { }
method2() { }
}
or simply call the method directly inside the other method
if(generateNumber() == 3){
System.out.println("3 has been counted.");
}
精彩评论