简体   繁体   中英

Java calling a method with object parameters to main method

play.java: https://pastebin.com/4bzfE76z
values.java: https://pastebin.com/6mnUzKyA
player.java: https://pastebin.com/qiFymMF6
stats.java: https://pastebin.com/P24AUpXV

I have a method called start in play.java with object parameters. The objects were originally declared in values.java inside the sValues method. I want to call the method start to the main method of play.java but I'm not sure what to write inside the parameters of the call.

Inside values.java :

    player user = new player ();
    user.setP_Name (username);
    user.setP_ATK (atk);
    user.setP_HP (hp);

    stats[] enemies = new stats [3];
    enemies [1] = new stats ();
    enemies [1] = gob;
    enemies [1].getName ();
    enemies [1].getHP ();
    enemies [1].getATK ();
    enemies [1].getMANA ();
    enemies [2] = new stats ();
    enemies [2] = orc;
    enemies [2].getName ();
    enemies [2].getHP ();
    enemies [2].getATK ();
    enemies [2].getMANA ();

Inside play.java , I use the objects like this:

System.out.println ("Hitpoints[HP]: " + enemies [i].getHP ());

and it works great because I included these two objects into the parameter of the method:

public static void start (player user, stats[] enemies)

However, whenever I want to call start in the main method for it to do the things I want it to, it gives me errors. This is how I've been trying to call it:

public static void main (String[] args)
{ //main method
start(player, stats);
} //main method

Am I doing something wrong? Any help appreciated. I'll gladly add more information if needed. New to coding and not sure what specific details need to be provided.

My errors:

{ //main method
    player player = new player();
    start(player, stats);
} //main method

Error on: start(player, stats ); Cannot find symbol. Symbol: variable stats. Location: class play.

This error is given when I hover over "stats" when using netbeans.

The main method is the entry point of your program. You have not instantiated anything else yet so there are no objects to pass into your start method.

You might need to give us more of your code, but you might need to try something like this first - and I this is making a lot of assumptions about your code

public static void main (String[] args)
{ //main method
    Player player = new Player(); // This is an instance of your Player class
    // and initialize your stats array.
    Values v = new Values();
    start(player, stats);
} //main method

Also providing the error you are getting may help also.

EDIT

After reviewing your code I can see some problems. In your Values class you have the method sValues which creates the enemies as you said, however they stats[] that you created in that method only has the scope of that method, so when that method is finished, the enemies you created have now disappeared.

You also have a lot of static reference - this is bad practice and you need to remove as much as possible. To do this you can change something like

 Values.sValues();

to

 Values v = new Values(); // in the constructor call what you need to
 v.sValues();

You need to think about where you are going to store your enemies stats array. You could move it to the top of your Values class and add a getter for it then your code could look like this

Values.java

public class values {
    public static String clear = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    public static String username;
    public static int atk = 1;
    public static int hp = 10;

    private stats[] enemies;

 // .. your other code
public void sValues () // this method should not be static
{ //sValue method 
    // .. other code
    enemies = new stats [3];  // Remove the declaration at the start of this line
    // .. other code
}

public stats[] getEnemies()
{
    return enemies;
}

Play.java

public static void main (String[] args)
{
    // Create a player
    player player = new player("Steve", 10, 10);
    // Initialise your values
    Values v = new Values();
    // create the enemies (this could be done in your values constructor)
    v.sValues();
    // Start with these enemies
    start(player, v.getEnemies());
}

Seems to me you have to create an instance of values.java and then get the player and stats objects from there to pass on to the main method. where are player, stats being declared in the play.java class?

try:

{
  player player = new player();
  stats[] stats = new stats[5];

  start(player, stats);
} //main method

and initialize stats array with desired values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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