Reputation: 27
i have this two codes:
game.java
package game;
import java.util.*;
public class games {
public static Random rd = new Random();
public static void main(String[] args){
//enemies
enemy skeleton = new enemy(1, "jarda" , 2 , 3);
enemy kostlivec = new enemy(2, "pepa" , 2 , 3);
}
}
and enemy.java
package game;
import java.util.jar.Attributes.Name;
public class enemy {
public static int number;
public static String název;
public static int damage;
public static int health;
public enemy(int number, String název, int damage, int health){
this.number = number;
this.název = název;
this.damage = damage;
this.health = health;
}
}
How can i randomly select one of these objects and print it to user ? I try arraylist, arrays and many others.
Upvotes: 0
Views: 68
Reputation: 5882
As pointed out in comments, you should use random.nextInt()
with upper bound. You can create a List<Enemy>
and pass pass it's size()
as upperbound. Here is a simple implementation:
private static Enemy getRandomEnemy(final List<Enemy> enemies) {
int randomEnemyIndex = rd.nextInt(enemies.size());
return enemies.get(randomEnemyIndex);
}
You would call it from main()
method like this:
ArrayList<Enemy> enemies = new ArrayList<>();
// enemies
enemies.add(new Enemy(1, "jarda" , 2 , 3));
enemies.add(new Enemy(2, "pepa" , 2 , 3));
enemies.add(new Enemy(3, "Palpatine" , 10 , 3));
enemies.add(new Enemy(4, "Darth Vader" , 9 , 10));
System.out.println("Current Enemy: " + getRandomEnemy(enemies).název);
System.out.println("Current Enemy: " + getRandomEnemy(enemies).název);
System.out.println("Current Enemy: " + getRandomEnemy(enemies).název);
System.out.println("Current Enemy: " + getRandomEnemy(enemies).název);
Bdw, Some nitpicks for Enemy
class; By convention Java class names should start with an uppercase character. I think it should have non static field members. Otherwise, I don't see any point in storing different enemies:
public class Enemy {
public int number;
public String název;
public int damage;
public int health;
public Enemy(int number, String název, int damage, int health){
this.number = number;
this.název = název;
this.damage = damage;
this.health = health;
}
}
Upvotes: 1
Reputation: 2486
As described in the comment:
public class Testing {
public static void main(String[] args) {
MyObject obj1 = new MyObject(1, 2);
MyObject obj2 = new MyObject(3, 4);
ArrayList<MyObject> myList = new ArrayList<>();
myList.add(obj1);
myList.add(obj2);
Random rand = new Random();
int randResult = rand.nextInt(myList.size());
System.out.println(myList.get(randResult));
}
}
class MyObject {
private int value;
private int anotherValue;
MyObject(int v1, int v2) {
value = v1;
anotherValue = v2;
}
@Override
public String toString() {
return "MyObject with values: " + value + ", " + anotherValue;
}
}
Output:
MyObject with values: 1, 2
Upvotes: 0
Reputation: 437
Add Object into a List, and get Index Random by Random.nextInt(list.size()) Code
package com.game.main;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.game.model.Enemy;
public class Games {
public static void main(String[] args) {
Enemy skeleton = new Enemy(1, "jarda", 2, 3);
Enemy kostlivec = new Enemy(2, "pepa", 2, 3);
List<Enemy> list = new ArrayList<>();
list.add(skeleton);
list.add(kostlivec);
Random random = new Random();
int indexRandom = random.nextInt(list.size());
System.out.println(list.get(indexRandom));
}
}
Upvotes: 1