构造函数使我的代码变得丑陋,并让我输入不相关的代码,JAVA

时间:2014-01-16 14:56:22

标签: java constructor

“blah”,“blah”,1,1是无关紧要的,因为我没有使用它 我只是输入了一些随机字符串和int,因为java要求我这样做 因为构造函数Pokemon()。 顺便说一句,在这个任务中,我们不允许使用静态 我需要这样做所以我可以使用“战斗”方法,因为我需要做一个实例。 我也试过使用一个随机的已创建对象,如 方[0] .fight(TEAM1,TEAM2);但它更丑陋,看起来更无关紧要。

package pokemon;
import java.util.Random;


public class PokemonMain {
    public static void main(String[] args){
        Pokemon[] party = new Pokemon[6];
        Pokemon[] party2 = new Pokemon[6];
        party[0] = new Pokemon("pikachu", "electric", 500, 1);
        party[1] = new Pokemon("raichu", "electric", 500, 1);
        party[2] = new Pokemon("charmander", "water", 500, 1);
        party[3] = new Pokemon("magikarp", "electric", 500, 1);
        party[4] = new Pokemon("bulbasaur", "electric", 500, 1);
        party[5] = new Pokemon("kabuto", "electric", 500, 1);

        party2[0] = new Pokemon("pikachu2", "electric", 500, 1);
        party2[1] = new Pokemon("raichu2", "electric", 500, 1);
        party2[2] = new Pokemon("charmander2", "water", 500, 1);
        party2[3] = new Pokemon("magikarp2", "electric", 500, 1);
        party2[4] = new Pokemon("bulbasaur2", "electric", 500, 1);
        party2[5] = new Pokemon("kabuto2", "electric", 500, 1);

        //Picks random pockemons given an array of pokemons
        Random randomGenerator = new Random(); 
        int randomInt = randomGenerator.nextInt(6);
        Pokemon team1 = party[randomInt];
        randomInt = randomGenerator.nextInt(6);
        Pokemon team2 = party2[randomInt];

        Pokemon forTheSakeOfTheMethod = new Pokemon("blah","blah",1,1);
        //the "blah","blah",1,1  is irrelevant since i dont get to use it
        // i just inputted some random string and int because java asks me to do so
        //because of the constructor Pokemon().
        //btw in this task, we we're not allowed to use static
        //and i needed to do this so i can use "fight"method because i needed to make
          an
        //instance.
        // i also tried just using a random already-created object like
        // party[0].fight(team1,team2); but its uglier and looks more irrelevant.
        forTheSakeOfTheMethod.fight(team1,team2);

======================================================================
package pokemon;
import java.util.Random;
import java.util.Scanner;

public class Pokemon {
    private String name;
    private String type;
    private int hp, exp, atk, def, xdef, xatk, spd, level;
    public static String region ="Kanto";
    public int generation;
    public int[] hello = new int[5];


    private static int numberOfPokemons = 0;

    public Pokemon(String name, String type, int hp, int level){
        this.name = name;
        this.type = type;
        this.hp = hp;
        this.level = level;
    }

    public void fight(*parameters*){
        //something
    }

3 个答案:

答案 0 :(得分:3)

你的快速修复是添加一个无参数构造函数(按照@Grammin)但是我个人认为你可能想要做的是将'fight'方法更改为只有一个参数。 e.g。

public class Pokemon
{
  ...
  public int fight(Pokemon team2) {
    Pokemon team1 = this;
    ...
    return winner;
  }
}

然后您可以像这样使用它:

team1.fight(team2);

答案 1 :(得分:1)

您可以更改Pokemon类/构造函数,也可以创建另一个处理战斗的类。

public class PokemonFightManager
{
   public int fight(Pokemon pokemon1, Pokemon pokemon2)
   {
       //do something and return result
   }

   public int fightTeam(Pokemon[] team1, Pokemon[] team2)
   {
       //do something and return result
   }
}

答案 2 :(得分:-1)

你可以在Pokemon中创建另一个不带任何参数的构造函数:

public class Pokemon {
  public Pokemon(){}
}

但我怀疑你需要你的名字/类型/ hp /等级的战斗方法,如果它是这样提供给你的。