简体   繁体   English

传递参数(新手)(Java)

[英]Passing Parameters (Novice) (Java)

I'm fairly beginner when it comes to Java, as I've focused more on Js for University rather than the OOP-heavy brother of it and as a result, I can't quite pass my parameters properly. 在谈到Java时,我还是个初学者,因为我将更多的精力放在大学的Js上,而不是Java的面向对象的兄弟,因此,我无法完全传递我的参数。

I'm looking to generate a random number in 'generateMonsterCode' and pass it into 'chooseMonster', but I can't quite grasp how to accomplish this. 我希望在“ generateMonsterCode”中生成一个随机数,并将其传递给“ chooseMonster”,但是我不太了解如何完成此操作。

/**
 * Randomnly choose a type of monster of varying hp/mp/loot drops
 * @return rndGen
 */
public int generateMonsterCode (){
    Random rndGens = new Random();
    int rndGen = 0;

    for (int i = 1; i < 2; i++) {
       rndGen = rndGens.nextInt(4) + 1;
    }

    return rndGen;
}

/**
 * Define which monster is chosen
 */
public void chooseMonster(){

    switch(rndGen){
        case 1: System.out.println("Monster:" + rndGen);
        case 2: System.out.println("Monster:" + rndGen);
        case 3: System.out.println("Monster:" + rndGen);
        case 4: System.out.println("Monster:" + rndGen);
    }     
}

Any help would be massively appreciated! 任何帮助将不胜感激!

Updated regarding feedback 有关反馈的更新

    /**
 * Randomnly choose a type of monster of varying hp/mp/loot drops
 * @return rndGen
 */

public void generateMonsterCode (){
Random rndGens = new Random();
    int rndGen = 0;

    rndGen = rndGens.nextInt(4) + 1;

    chooseMonster(rndGen);

}

/**
 * Define which monster is chosen
 */

private void chooseMonster(int rndGen){

    switch(rndGen){
    case 1: System.out.println("Monster:" + rndGen);
            break;
    case 2: System.out.println("Monster:" + rndGen);
            break;
    case 3: System.out.println("Monster:" + rndGen);
            break;
    case 4: System.out.println("Monster:" + rndGen);
            break;
    }

}

Would this perhaps be better code practice? 这可能是更好的代码实践吗?

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()是不带参数的方法。

public void chooseMonster(int i){ takes an int as a parameter and names it i in the scope of the method. public void chooseMonster(int i){将int作为参数,并在方法范围内将其命名为i

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) . 因此,在generateMonsterCode() ,生成随机数后,您只需调用chooseMonster(rndgen)

Within that method, you can refer to the int you passed into it with: 在该方法中,您可以使用以下方法引用传递给它的int

i.doSomething() ; 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: 另一种结构化方法是让您的generateMonsterCode()方法返回一个int值:

public int generateMonsterCode(){ 
    .... generate random number
    return rdngen();

and call it at the beginning of your chooseMonster() method. 并在chooseMonster()方法的开头调用它。

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. 仅供参考:@supericy对您在随机代中使用的for循环是正确的。 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. 而且您可能还应该检查public访问修饰符的作用以及您是否真正需要(可能不需要),但是这些问题不在您的讨论范围之内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM