- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个“世界模拟器”程序。但我不知道如何访问每个键的所有值。在我的世界类中,我尝试执行方法“drawWorld”,但是每当两个或多个生物体位于同一个池中时,我的程序就看不到这一点。示例输出:
0 [F2][F4][F8] 4 5 6 7 8 [F1]
10 11 12 13 [F5] 15 16 17 18 19
[F7] 21 22 23 24 25 26 27 28 29
30 [F6] 32 33 34 35 [F3] 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
其中每个 F(F1、F2 等)都是 Fox 对象,在这个特定示例中,F2、F4 和 F8 位于同一池(在世界地图中),但程序将其显示为位于不同池中(而不是 1 [ F2][F4][F8] 4 5 应为 1 [F2,F4,F8] 3 4 )。你能告诉我我哪里出错了吗?我确信我的逻辑是:world.checkIfPoolHasOneOrMultipleOrganisms 很糟糕,问题是我不确定出了什么问题
import Obiektowe.WorldSimulator.Animal.Fox;
public class WorldSimulatorDemo {
public static void main(String[] args) {
World world = new World();
world.drawWorld();
Organism fox = new Fox(world, 10, 10);
Organism foxxy = new Fox(world, 10, 10);
Organism foxter = new Fox(world, 10, 10);
Organism foxtser = new Fox(world, 10, 10);
Organism foxtedr = new Fox(world, 10, 10);
Organism foxterd = new Fox(world, 10, 10);
Organism foxtera = new Fox(world, 10, 10);
Organism foxtesr = new Fox(world, 10, 10);
for (int i = 0; i < 2; i++) {
world.makeTurn();
world.drawWorld();
}
}
}
package Obiektowe.WorldSimulator;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import lombok.Data;
import java.util.*;
@Data
public class World {
List<Organism> organisms;
Multimap<Integer, Organism> worldMap;
private static boolean wasPopulatedWithEmptyEntries = false;
public World() {
this.organisms = new ArrayList<>();
this.worldMap = ArrayListMultimap.create();
populateMapWithEmptyEntries();
}
public void addOrganismToWorldMap(Organism o) {
Random random = new Random();
int randomNumber = random.nextInt(10);
worldMap.put(randomNumber, o);
}
public void makeTurn() {
System.out.println("\n ***** NEW ROUND ***** \n");
for (Organism o : organisms) {
o.action();
}
}
public boolean defineIfOrganismsOnTheSamePoolAreSameType(int pool) {
return false;
}
protected void drawWorld() {
int counter = 0;
for (Map.Entry<Integer, Organism> entry : worldMap.entries()) {
if (counter > 99
) {
break;
}
if (counter % 10 == 0) {
System.out.println();
}
if (entry.getValue() == null) {
System.out.print(" " + counter + " ");
} else {
// System.out.print(entry.getValue() + ",");
checkIfPoolHasOneOrMultipleOrganisms(entry.getValue());
}
counter++;
}
}
private void checkIfPoolHasOneOrMultipleOrganisms(Organism entryValue) {
List<String> organismList = new LinkedList<>();
StringBuilder stringBuilder = new StringBuilder();
String formatedValue;
organismList.add(entryValue.toString());
if (organismList.size() == 1) {
System.out.print(organismList);
}
if (organismList.size() == 2) {
for (String string : organismList) {
stringBuilder
.append(string)
.append(",");
}
formatedValue = stringBuilder.toString();
System.out.print(formatedValue);
}
}
private void populateMapWithEmptyEntries() {
for (int i = 0; i < 100; i++) {
worldMap.put(i, null);
}
}
}
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Data
public abstract class Organism {
protected World world;
protected int strength;
protected int speed;
public Organism(final World world, final int strength, final int speed) {
this.world = world;
this.strength = strength;
this.speed = speed;
getWorld().getOrganisms().add(this);
}
protected abstract void action();
protected abstract void collision();
protected abstract void draw();
}
package Obiektowe.WorldSimulator.Animal;
import Obiektowe.WorldSimulator.Organism;
import Obiektowe.WorldSimulator.World;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.Random;
@Getter
@Setter
@Data
public abstract class Animal extends Organism {
public Animal(final World world, final int strength, final int speed) {
super(world, strength, speed);
// this.coordinates = world.worldMap[worldCoordinateX][worldCoordinateY];
}
@Override
protected void action() {
}
@Override
protected abstract void collision();
protected abstract void move();
@Override
protected abstract void draw();
@Override
public String toString() {
return "Animal";
}
}
package Obiektowe.WorldSimulator.Animal;
import Obiektowe.WorldSimulator.Organism;
import Obiektowe.WorldSimulator.World;
import lombok.Data;
import java.util.Map;
import java.util.Random;
@Data
public class Fox extends Animal {
protected int id;
protected static int idCcounter;
private boolean isAdded = false;
public Fox(final World world, final int strength, final int speed) {
super(world, strength, speed);
idCcounter++;
id = idCcounter;
}
@Override
protected void action() {
checkIfOrganismWasAddedToWorldMap();
move();
}
@Override
protected void collision() {
}
@Override
protected void move() {
int currentOrganismPosition = 0;
int newOrganismPosition;
for (Map.Entry<Integer, Organism> entry : getWorld().getWorldMap().entries()) {
if (entry.getValue() != null && entry.getValue().equals(this)) {
currentOrganismPosition = entry.getKey();
}
}
newOrganismPosition = randomlyChangePosition(currentOrganismPosition);
getWorld().getWorldMap().remove(currentOrganismPosition, this);
getWorld().getWorldMap().put(newOrganismPosition, this);
}
private int randomlyChangePosition(int currentOrganismPosition) {
final int NUMBER_TO_MOVE_LEFT_BY_1 = -1;
final int NUMBER_TO_MOVE_RIGHT_BY_1 = 1;
final int NUMBER_TO_MOVE_UP_BY_1 = -10;
final int NUMBER_TO_MOVE_DOWN_BY_1 = 10;
final int MIN_WORLDMAP_POSITION = 0;
final int MAX_WORLDMAP_POSITION = 99;
Random random = new Random();
int randomNumber = random.nextInt(4) + 1;
int newPosition;
switch (randomNumber) {
case 1:
//Move left
newPosition = NUMBER_TO_MOVE_LEFT_BY_1;
break;
case 2:
//Move right
newPosition = NUMBER_TO_MOVE_RIGHT_BY_1;
break;
case 3:
//Move up
newPosition = NUMBER_TO_MOVE_UP_BY_1;
break;
case 4:
//Move down
newPosition = NUMBER_TO_MOVE_DOWN_BY_1;
break;
default:
newPosition = 0;
}
if (currentOrganismPosition + newPosition >= MIN_WORLDMAP_POSITION &&
currentOrganismPosition + newPosition <= MAX_WORLDMAP_POSITION) {
return currentOrganismPosition + newPosition;
} else {
return 0;
}
}
@Override
protected void draw() {
}
protected void checkIfOrganismWasAddedToWorldMap() {
if (!isAdded) {
getWorld().addOrganismToWorldMap(this);
isAdded = true;
}
}
@Override
public String toString() {
return "F" + id;
}
}
最佳答案
我将方法更改为:
protected void drawWorld() {
int counter = 0;
for (Map.Entry<Integer, Collection<Organism>> entry : worldMap.asMap().entrySet()) {
Collection<Organism> valuesForKey = entry.getValue();
if (counter % 10 == 0) {
System.out.println();
}
if (valuesForKey.size() == 1) {
System.out.print(" " + counter + " ");
} else {
printValuesWithoutNullEntry(valuesForKey);
}
counter++;
}
}
private void printValuesWithoutNullEntry(Collection<Organism> organisms) {
final int INDEX_WHERE_NULL_PHRASE_ENDS = 7;
System.out.print("[" + organisms.toString().substring(INDEX_WHERE_NULL_PHRASE_ENDS));
}
现在它可以工作了,现在我只需要找到一种不在空条目处显示空值的方法,它应该容易得多:)
关于java - 如何访问 Guava 多重映射的所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61351091/
请看一下我的代码。 int main () { Program* allcommand = new Program; allcommand->addCommand("add", new
因此,当我遇到调试断言时,我正在编写代码。现在我很想知道为什么这段代码不起作用: for(Model::MeshMap::iterator it = obj1->GetMeshes().begin()
这是我上一个问题的延续 Group, Sum byType then get diff using Java streams . 按照建议,我应该作为单独的线程发布,而不是更新原始线程。 因此,通过我
我正在实现一些非常适合 map 的代码。但是,我要迭代的列表中有大量对象,所以我的问题是哪种方法是解决此问题的最佳方法: var stuff = $.map(listOfMyObjects, some
我正在尝试创建一个包含不同类的成员函数指针的映射。成员函数都具有相同的签名。为了做到这一点,我所有的类都继承了一个 Object 类,它只有默认构造函数、虚拟析构函数和一个虚拟 ToString()
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: how do you make a heterogeneous boost::map? 有可能在 C++ 中
我有一个 Mysql 查询,请检查以下内容: SELECT `tbl_classSubjects`.`classID` , `tbl_classSubjects`.`sectionID` , `tbl
抱歉,这可能是一个基本问题。 JNA直接映射和接口(interface)映射有什么区别? 我的解释是否正确: 直接映射 : 直接使用库对象(如 Java 中的静态 main) 接口(interface
在 Twitter's Scala school collections section ,它们显示了一个带有偏函数作为值的 Map: // timesTwo() was defined earlie
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
据我了解,从 scala stdlib 声明一个映射并没有将其专门用于原始类型。我要的不是付出装箱/拆箱的代价,而是同时拥有scala map 的接口(interface)。一个明显的选择是使用 tr
如何为这样的 JSON 响应创建对象映射,它只是一个整数数组: [ 565195, 565309, 565261, 565515, 565292, 565281, 566346, 5
是否可以为 DTO 对象创建映射然后查询它们 而不是域?如果不解释为什么? 如果我需要几个 dtos 怎么办? DTos 是只读的 ID 由 NH 自动生成 将来这些 dtos 将设置映射到链接的 d
我有一个返回的函数(常规代码) [words: "one two", row: 23, col: 45] 在 Scala 中,我将上面更改为 Scala Map,但随后我被迫将其声明为 Map[Str
我有一组与 Vanilla 磅蛋糕烘焙相关的数据(200 行),具有 27 个特征,如下所示。标签caketaste是衡量烤蛋糕的好坏程度,由 bad(0) 定义, neutral(1) , good
我有试图映射到新代码的遗留代码。 OLD_PERSON pid sid name age NEW_PERSON pid sid fid age RESOLVE_PERSON pid fid statu
我有一个表,其中一个字段可以指向其他 3 个表之一中的外键,具体取决于鉴别器值是什么(Project、TimeKeep 或 CostCenter。通常这是用子类实现的,我想知道我有什么 注意子类名称与
我有一个类型 [ST s (Int, [Int])] 的绑定(bind)我正在尝试申请runST使用映射到每个元素,如下所示: name :: [ST s (Int, [Int])] --Of Cou
在我正在进行的项目中,我有以下实体:分析师、客户 和承包商。每个都继承自基类 User。 public abstract class User { public virtual int Id
我想知道是否可以在 Vim 中创建一个映射(对于普通模式),允许用户在映射执行之前输入。 我想为我最常用的 grep 命令创建一个快捷方式的映射。我希望命令允许输入我正在搜索的内容,然后在输入时执行。
我是一名优秀的程序员,十分优秀!