Parameters are specified by placing them in the parentheses beside the method declaration.
public void chooseMonster()
is a method that takes no parameters.
public void chooseMonster(int i){
takes an int as a parameter and names it i
in the scope of the method.
When you specify a parameter for a method, you then must pass a variable of the type specified when calling it. So, in generateMonsterCode()
, after you generate the random number, you'll simply call chooseMonster(rndgen)
.
Within that method, you can refer to the int
you passed into it with:
i.doSomething()
;
Does this Make sense? Let me know if I can explain any of this more carefully: I'm brushing against some of the fundamental aspects of object-oriented programming here, and it's important that you grasp these concepts if you want to have a role in the future.
FYI: On the other end of the method declaration is the return type - in your case, it's void, meaning that nothing is returned. An alternative way of structuring this would be to have your generateMonsterCode()
method return an int:
public int generateMonsterCode(){
.... generate random number
return rdngen();
and call it at the beginning of your chooseMonster()
method.
public void chooseMonster() {
int i = generateMonsterCode();
... do work
}
Note that this is not necessarily better or worse than doing it the other way, I'm just mentioning it for purposes of explanation.
Also FYI: @supericy is right about the for loop you're using in your random generation. And you should also probably examine what the public
access modifier does and whether you really need it (you probably don't) - but these issues are outside the scope of your question.