英文:
Java -random select obejct
问题
我有这两段代码:
game.java
```java
package game;
import java.util.*;
public class games {
public static Random rd = new Random();
public static void main(String[] args){
//敌人
enemy skeleton = new enemy(1, "jarda" , 2 , 3);
enemy kostlivec = new enemy(2, "pepa" , 2 , 3);
}
}
和 enemy.java
package game;
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;
}
}
如何随机选择其中一个对象并将其打印给用户?
我尝试过 ArrayList、数组和许多其他方法。
<details>
<summary>英文:</summary>
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.
</details>
# 答案1
**得分**: 1
```java
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));
}
}
英文:
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));
}
}
答案2
得分: 1
正如评论中指出的,你应该使用带有上限的 random.nextInt()
。你可以创建一个 List<Enemy>
,并将其 size()
作为上限传递。以下是一个简单的实现:
private static Enemy getRandomEnemy(final List<Enemy> enemies) {
int randomEnemyIndex = rd.nextInt(enemies.size());
return enemies.get(randomEnemyIndex);
}
你可以在 main()
方法中这样调用它:
ArrayList<Enemy> enemies = new ArrayList<>();
// 添加敌人
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("当前敌人: " + getRandomEnemy(enemies).název);
System.out.println("当前敌人: " + getRandomEnemy(enemies).název);
System.out.println("当前敌人: " + getRandomEnemy(enemies).název);
System.out.println("当前敌人: " + getRandomEnemy(enemies).název);
另外,关于 Enemy
类有一些小问题;按照惯例,Java 类名应以大写字符开头。我认为它应该有非静态字段成员。否则,我不明白存储不同敌人的意义:
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;
}
}
英文:
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;
}
}
答案3
得分: 0
按照注释所述:
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;
}
}
输出:
MyObject with values: 1, 2
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论