如何从另一个方法获取 return 方法中的参数值?

How to get parameter's value in a return method from another method?

我在MovePath()方法中的int dice需要在GetRollDice()方法中获取int step的值。

我正在使用 Java。

我应该怎么做?

public static int getRollDice(){
   int[] diceStep= {-2,-1,1,2,3};            
   int randomStep = new Random().nextInt(diceStep.length); 
   int step = diceStep[randomStep];
   return step;
}

public static int MovePath(Integer dice,Integer path){
  // int dice = step value from GetRollDice      
  //Get Initial Path
  //Next path = roll dice value + initial path


    return path;

}

你只需要调用你的方法,不需要重新声明 dice :

public static int MovePath(Integer dice,Integer path){
    dice = getRollDice();     
    //Get Initial Path
    //Next path = roll dice value + initial path

    return path;
}

请记住,Java 中的所有内容都是对 Object 的引用...除了 intshort 等基本类型之外的所有内容...

有关这方面的更多信息,请参阅 What is the difference between Integer and int in Java?