【问题标题】:How to get parameter's value in a return method from another method?如何从另一个方法获取返回方法中的参数值?
【发布时间】:2015-11-24 17:08:22
【问题描述】:

我在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;

}

【问题讨论】:

  • 我们是否应该猜测您正在使用的编程语言?如果是这样,如果认为它是一些 Java,对吗?
  • @dkg 很抱歉。是的,它是 java

标签: java parameters parameter-passing return-value


【解决方案1】:

你只需要调用你的方法,不需要重新声明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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多