gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:51:05 24 4
gpt4 key购买 nike

我的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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com