Home > Mobile >  How can I use the return value of a method as a parameter in another method?
How can I use the return value of a method as a parameter in another method?

Time:11-15

I have this command Out.print(getWin(dice, bet, bid)); but Java doesn't seem to know the 3 values dice, bet and bid as they are return values of other methods and therefore not known in the Main Method.

Now I know that there is the possibility to call up the whole functions to give the return values as parameters Out.print(getWin(rollTheDice(), givenBet(), givenBid())); but the big problem is that the exercise I am working on, requires me to include lines like System.Out.Print("Amount of Bid: "); into the functions itself which means that when I call up the functions as parameters it starts printing out code and asking the user to enter data again and I am trapped in an endless loop instead of getting the return value of the function getWin which is the thing I actually want.

Is there any way I can pass on the parameters as variables like suggested in the 1st row of the question? Maybe by initializing them outside the function (although I already tried that as well and it didn't work either)? Otherwise I am starting to think this exercise isn't really doable the way I am supposed to do it.

CodePudding user response:

I was able to fix it:

The clue is to assign the functions to variables as the commenter suggested and at the same time executing the function:

int dice = rollTheDice();
int bet = givenBet();
int bid = givenBid();
getWin(dice, bet, bid);
  • Related