[英]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 another method, in the same class file, in an if-statement.现在,我想在 if 语句中将 randomnum int 值访问到另一个方法中,在同一个 class 文件中。 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 scope 中声明 int 然后所有方法都可以访问它
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.");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.