如何调用传递了变量的方法?

我正在尝试从另一个方法中调用一个方法。我很简单地理解了这一点,直到其中一种方法需要传递一个变量,然后我尝试的任何方法都无法正常工作。

我知道我可以用一种方法来做到这一点,但是我的课程需要我以这种方式进行布局。为什么这行不通?

public class test2 {
   public static void testMethod() {
      int randomNumber = 1;
   }
   public static void anotherTestMethod(int randomNumber) {
      System.out.println(randomNumber);
   }
   public static void main(String[] args) {
      anotherTestMethod();
   }
}
uyi745 回答:如何调用传递了变量的方法?

您正在调用签名中带有int参数的方法。调用方法时应传递该参数。我认为您正在尝试使用全局变量,在这种情况下,应将其声明为类的一部分。

,
public class test2 {
   public static int testMethod() {
      int randomNumber = 1;
      return randomNumber;
   }
   public static void anotherTestMethod() {
      System.out.println(testMethod());
   }

   public static void main(String[] args) {
      anotherTestMethod();
   }
}
本文链接:https://www.f2er.com/3169591.html

大家都在问