![](/img/trans.png)
[英]How to call a method and use it's return value as an another method parameter?
[英]How can I use the return value of a method as a parameter in another method?
我有这个命令Out.print(getWin(dice, bet, bid));
但是 Java 似乎不知道 3 个值dice
, bet
和bid
因为它们是其他方法的返回值,因此在 Main Method 中不知道。
现在我知道有可能调用整个函数来给出返回值作为参数Out.print(getWin(rollTheDice(), givenBet(), givenBid()));
但最大的问题是我正在做的练习,需要我包括像System.Out.Print("Amount of Bid: ");
进入函数本身,这意味着当我调用函数作为参数时,它开始打印代码并要求用户再次输入数据,我陷入了无限循环,而不是获取函数getWin
的返回值。我其实想要。
有什么方法可以将参数作为变量传递,如问题第一行中建议的那样? 也许通过在函数之外初始化它们(虽然我也已经尝试过,但它也不起作用)? 否则我开始认为这个练习并不像我应该做的那样真正可行。
我能够修复它:
线索是按照评论者的建议将函数分配给变量,同时执行函数:
int dice = rollTheDice();
int bet = givenBet();
int bid = givenBid();
getWin(dice, bet, bid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.