类,它从主数组中移除对象

huangapple go评论60阅读模式
英文:

Class which removes objects from array in main

问题

zombieHorde.java

public class ZombieHorde {
    int hordeSize = 0;
    Zombie[] zombies; // Define an array to hold zombies
    
    public ZombieHorde(int hordeSize) {
        zombies = new Zombie[hordeSize]; // Initialize the array
        this.hordeSize = hordeSize; // Update the hordeSize
    }
}

zombie.java

public class Zombie {
    String name;
    int limbs;

    public Zombie(String name, int limbs) {
        this.name = name;
        this.limbs = limbs;
    }

    public void loseLimbs() {
        limbs--;
    }

    public int getLimbs() {
        return limbs;
    }

    public void leaveHorde() {
        hordeSize--; // This line seems incorrect, as hordeSize is not defined here
    }
}

main.java

import Zombie;
import ZombieHorde;
import ZombieHunter;

public class MyZombieGame {
    public static void main(String[] args) {
        int hordeSize = 10; // Define the horde size
        ZombieHorde h = new ZombieHorde(hordeSize); // Correctly instantiate ZombieHorde

        Zombie myZombie = new Zombie("Chuck", 4); // Instantiate a zombie

        ZombieHunter zombieHunter = new ZombieHunter(); // Instantiate a zombie hunter
        zombieHunter.shootAt(myZombie.name); // Call the shootAt method on the zombie hunter
    }
}

zombiehunter.java

public class ZombieHunter {
    public void shootAt(String name) {
        // Logic to compare accuracy and random number and remove zombie
    }
}

Please note that your code had a few issues, like using the hordeSize variable in the wrong context and some missing variable declarations. In the provided translations, I've made corrections and provided a basic structure for the shootAt method in the ZombieHunter class. You can implement the logic to compare accuracy and remove zombies based on your described approach.

英文:

I'm working on an object-oriented program in Java that allows you to keep track of a horde of zombies. A zombie can be identified either by a unique id number or a single name, and we want to note how many limbs each zombie currently possesses. Zombies may leave the horde either by their own accord or removed by a zombie hunter.

Currently I'm trying to work on the zombie hunter class, I need it to remove objects from an array of zombies in main after it meets a certain condition, and the other classes should be done I believe:

zombieHorde.java

public class ZombieHorde{
  int hordeSize = 0;

  public ZombieHorde(int hordeSize){
    Zombie zombies = new Zombie[hordeSize];
    hordeSize++;
  }
}

zombie.java

import horde;

public class Zombie {
  int hordeSize = 0;

  public Zombie(String name, int limbs) {
    this.name = name;
    this.limbs = limbs;
  }

  public int loseLimbs() {
    limbs--;
  }

  public int getLimbs() {
    return limbs;
  }

  public void leaveHorde() {
    hordeSize--;
  }
}

main.java

// accuracy to determine if a zombie hunter kills its mark,
// then if it does I can decrease the horde size and get rid
// of the object/kill the zombie, use random for determining
// # of limbs, and if zombies join a horde, etc.
// main class creates a horde, creates zombies and adds them
// to a horde, and the zombie hunter object

// could also have multiple Hordes, ZombieHunters!

import Zombie;
import ZombieHorde;
import ZombieHunter;

public class MyZombieGame{
  public static void main(String[] args){
    // creates horde
    ZombieHorde h = ZombieHorde(hordeSize);

    // create zombies and add them to array
    Zombie myZombie = Zombie(Chuck, 4);

    // create zombie hunter to remove from array?
    
  }
}

And zombiehunter.java

import horde;

public class ZombieHunter{
  public void shootAt(name){

  }
}

What I'm thinking is I could have an accuracy assigned to a zombie hunter, and then compare that to a random number, and if the accuracy is higher, then the zombie hunter hits its mark and kills a zombie, then I remove a zombie object from the ZombieHorde array in main! If that makes sense, although I'm a little tripped up by them leaving on their own accords, any ideas would be awesome about an approach to this and how I should go about removing the objects from the array.

I'm brand new to Java so there might be some errors I apologize if this is the case! Just looking for a push in the right direction!

答案1

得分: 3

你可能会考虑使用一种映射(例如 Hashmap)来替代数组。

public class ZombieHorde{
  
  //注意,类属性通常是私有的
  private int hordeSize = 0; //实际上这里不需要 hordeSize,你可以直接使用 .size();
  private HashMap<String, Zombie> zombieHorde;

  public ZombieHorde(){
    //实例化 Hashmap
    this.zombieHorde = new HashMap<String, Zombie>();    
  }

  public void addToHorde(Zombie zombie){
     //假设已经实现了一个获取名字的方法
     this.zombieHorde.put(zombie.getName(), zombie);
     this.hordeSize++;
  }

  public void removeFromHorde(Zombie zombie){
     //假设已经实现了一个获取名字的方法
     this.zombieHorde.remove(zombie.getName());
     this.hordeSize--;
  }
}

在映射中,你可以使用类似添加(add)和移除(remove)的函数,这在像这样的用例中非常有用。如果你想知道 hordeSize,你也可以直接使用 zombieHorde.size()
(不确定我的代码是否100%正确,但你应该能理解思路)。

英文:

You might want to concider using some sort of map (i.e. a Hashmap) instead of an array.

public class ZombieHorde{
  
  //note that class attributes are usually private
  private int hordeSize = 0; //actually no need for hordSize here, you could just use .size();
  private HashMap&lt;String, Zombie&gt; zombieHorde;

  public ZombieHorde(){
    //instanciate Hashmap
    this.zombieHorde = new HashMap&lt;String, Zombie&gt;();    
  }

  public void addToHorde(Zombie zombie){
     //assuming there is a get name method implemented
     this.zombieHorde.put(zombie.getName, zombie);
     this.hordeSize++;
  }

  public void removeFromHorde(Zombie zombie){
     //assuming there is a get name method implemented
     this.zombieHorde.remove(zombie.getName);
     this.hordeSize--;
  }

}

On a map you can use functions like add and remoive which makes it useful for usecases like this. If you wanna know the hordeSize you can also just use zombieHodre.size()
(Not sure if my code is 100% correct but you should get the idea).

huangapple
  • 本文由 发表于 2020年9月15日 13:54:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63895903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定