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

Java 如何调用传递了变量的方法?,java,Java,我试图从另一个方法中调用一个方法。我对这一点的理解很简单,直到其中一个方法需要一个变量,然后我尝试的任何方法都不起作用 我知道我可以用一种方法做到这一点,但我的课程需要我以这样的方式来安排。为什么这样不行 public class test2 { public static void testMethod() { int randomNumber = 1; } public static void anotherTestMethod(int randomNumber

我试图从另一个方法中调用一个方法。我对这一点的理解很简单,直到其中一个方法需要一个变量,然后我尝试的任何方法都不起作用

我知道我可以用一种方法做到这一点,但我的课程需要我以这样的方式来安排。为什么这样不行

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();
   }
}

您正在调用签名中包含int参数的方法。调用该方法时应传递该参数。我认为您正在尝试使用全局变量,在这种情况下,您应该在任何方法之外声明它,作为类的一部分。

您的赋值是什么?我不清楚你被要求做什么。我有一个很好的猜测,但是让我们从马口中听到。好吧,你可以先把数字传递给这个方法。例如,
另一种测试方法(6)
。。。
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();
   }
}