Java -随机选择对象

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

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, &quot;jarda&quot; , 2 , 3);
enemy kostlivec = new enemy(2, &quot;pepa&quot; , 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&#225;zev, int damage, int health){
    this.number = number;
    this.n&#225;zev = n&#225;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, &quot;jarda&quot;, 2, 3);
		Enemy kostlivec = new Enemy(2, &quot;pepa&quot;, 2, 3);

		List&lt;Enemy&gt; list = new ArrayList&lt;&gt;();
		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&lt;Enemy&gt; and pass pass it's size() as upperbound. Here is a simple implementation:

private static Enemy getRandomEnemy(final List&lt;Enemy&gt; enemies) {
    int randomEnemyIndex = rd.nextInt(enemies.size());
    return  enemies.get(randomEnemyIndex);
}

You would call it from main() method like this:

ArrayList&lt;Enemy&gt; enemies = new ArrayList&lt;&gt;();
// enemies
enemies.add(new Enemy(1, &quot;jarda&quot; , 2 , 3));
enemies.add(new Enemy(2, &quot;pepa&quot; , 2 , 3));
enemies.add(new Enemy(3, &quot;Palpatine&quot; , 10 , 3));
enemies.add(new Enemy(4, &quot;Darth Vader&quot; , 9 , 10));

System.out.println(&quot;Current Enemy: &quot; + getRandomEnemy(enemies).n&#225;zev);
System.out.println(&quot;Current Enemy: &quot; + getRandomEnemy(enemies).n&#225;zev);
System.out.println(&quot;Current Enemy: &quot; + getRandomEnemy(enemies).n&#225;zev);
System.out.println(&quot;Current Enemy: &quot; + getRandomEnemy(enemies).n&#225;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&#225;zev;
    public int damage;
    public int health;

    public Enemy(int number, String n&#225;zev, int damage, int health){
        this.number = number;
        this.n&#225;zev = n&#225;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&lt;MyObject&gt; myList = new ArrayList&lt;&gt;();
        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 &quot;MyObject with values: &quot; + value + &quot;, &quot; + anotherValue;
    }
}

Output:

MyObject with values: 1, 2

huangapple
  • 本文由 发表于 2020年10月23日 17:45:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64497682.html
匿名

发表评论

匿名网友

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

确定