java - 如何从另一个方法的返回方法中获取参数的值?

标签 java parameters parameter-passing return-value

我的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?了解更多相关信息。

关于java - 如何从另一个方法的返回方法中获取参数的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33899722/

相关文章:

powershell - dotnet efscaffold 无法识别的选项 '-t firstTable -t secondTable' - 传递存储在字符串中的参数

java lambda :How to count total items in a list?

javascript - 查询参数中具有相同键的多个字段(axios 请求)?

c - 在 C 中将整数数组作为函数参数传递

android - 从 java 传递给 jni 的参数返回奇怪的值

Javascript 如何通过引用传递 int

java - 如何在 Solr 的 CSV 响应中打印您自己的列名称?

java - Java中的人脸识别

java - 找出符合条件的一串数字中的所有整数

javascript - 书签以嵌入带有参数的YouTube页面