Java 多次调用同一方法而不覆盖以前的值

Java 多次调用同一方法而不覆盖以前的值,java,Java,如果这对大多数人来说微不足道,我表示歉意,但我就是无法解决这个问题 我正在创建一个模拟游戏,在这里我有开始、结束和跳跃。有一些入口,如果你走在白色的入口上,你会向前跳得更远,而有一些黑色的入口,你会向后跳。我把这个班设为POJO private int totalSize; private int minDice; private int maxDice; private int whitePortalStart; private int whitePortalEnd; private int

如果这对大多数人来说微不足道,我表示歉意,但我就是无法解决这个问题

我正在创建一个模拟游戏,在这里我有开始、结束和跳跃。有一些入口,如果你走在白色的入口上,你会向前跳得更远,而有一些黑色的入口,你会向后跳。我把这个班设为POJO

private int totalSize;
private int minDice;
private int maxDice;
private int whitePortalStart;
private int whitePortalEnd;
private int blackPortalStart;
private int blackPortalEnd;
private int startPosition = 1;
private int currentPosition;

public GameObject(){}

public int getTotalSize() {
    return totalSize;
}

public void setTotalSize(int totalSize) throws Exception {
    if(totalSize <= 0){
        throw new Exception("Can't have a total distance of less than or equal to 0");
    } else {
        this.totalSize = totalSize;
    }
}

public int getMinDice() {
    return minDice;
}

public void setMinDice(int minDice) throws Exception {
    if(minDice <= 0){
        throw new Exception("Can't have a min dice value of less than or equal to 0");
    } else {
        this.minDice = minDice;
    }
}

public int getMaxDice() {
    return maxDice;
}

public void setMaxDice(int maxDice) throws Exception {
    if(getMinDice() > maxDice){
        throw new Exception("Cant have minimum dice number greater than the larger dice number");
    } else {
        this.maxDice = maxDice;
    }
}

public int getWhitePortalStart() {
    return whitePortalStart;
}

public void setWhitePortalStart(int whitePortalStart) throws Exception {
    this.whitePortalStart = whitePortalStart;
}

public int getWhitePortalEnd() {
    return whitePortalEnd;
}

public void setWhitePortalEnd(int whitePortalEnd) throws Exception {
    this.whitePortalEnd = whitePortalEnd;
}

public int getBlackPortalStart() {
    return blackPortalStart;
}

public void setBlackPortalStart(int blackPortalStart) throws Exception {
    this.blackPortalStart = blackPortalStart;
}

public int getBlackPortalEnd() {
    return blackPortalEnd;
}

public void setBlackPortalEnd(int blackPortalEnd) throws Exception {
    this.blackPortalEnd = blackPortalEnd;
}

public GameObject builder(int n) throws Exception {
    setTotalSize(n);
    return this;
}

public GameObject whitePortal(int m, int o) throws Exception {
    setWhitePortalStart(m);
    setWhitePortalEnd(o);
    return this;
}

public GameObject blackPortal(int o, int m) throws Exception {
    setBlackPortalStart(o);
    setBlackPortalEnd(m);
    return this;
}

public GameObject dice(int i, int j) throws Exception {
    setMinDice(i);
    setMaxDice(j);
    return this;
}

public int rollDice(){
    Random random = new Random();
    int min = getMinDice();
    int max = getMaxDice();

    return random.nextInt(max - min + 1) + min;
}

public void build(){
    int totalDistance = getTotalSize();
    currentPosition = startPosition;

    while(currentPosition < totalDistance){
        int diceValue = rollDice();

        if(currentPosition + diceValue > getTotalSize()){
            System.out.println("CurrentPosition : " + (currentPosition + diceValue) + ", is larger than the total size of the road - " + totalSize);
            continue;
        } else if(currentPosition + diceValue == getWhitePortalStart()){
            System.out.println("You landed on a white portal. Advancing from position " + (currentPosition + diceValue) + " to " + getWhitePortalEnd());
            currentPosition = getWhitePortalEnd();
        } else if(currentPosition + diceValue == getBlackPortalStart()){
            System.out.println("You landed on a black portal. Moving from position " + (currentPosition + diceValue) + " to " + getBlackPortalEnd());
            currentPosition = getBlackPortalEnd();
        } else {
            System.out.println("You landed on " + (currentPosition + diceValue));
            currentPosition += diceValue;
        }
    }
}
我的问题是当我想添加多个whitePortal/blackPortal时

WorldOfOz oz = new WorldOfOz();
    oz.go.builder(30)
        .dice(1, 4)
        .whitePortal(5, 12)
        .whitePortal(18, 26)
        .blackPortal(13, 2)
        .build();

值18-26超越5-12。我如何设置它,以便可以有多个黑白门户?

您的数据结构似乎不足以解决此问题

您需要定义一组白色门户和一组黑色门户。如果调用方法whitePortal5,12添加一个新的白色门户,而不是只设置现有白色门户的白色门户值

您需要定义一个类门户

然后您可以在游戏对象中使用它,如下所示:

public GameObject {
    List<Portal> whitePortals;
    List<Portal> blackPortals;


    public GameObject() {
        whitePortals = new ArrayList<Portal>();
        blackPortals = new ArrayList<Portal>();
     }

    public GameObject addWhitePortal(int m, int o) throws Exception {
        whitePortals.add(new Portal(m, o));
        return this;
    }

    ...

    // You need to change other methods to follow a different data structure 
}

您的数据结构似乎不足以解决此问题

您需要定义一组白色门户和一组黑色门户。如果调用方法whitePortal5,12添加一个新的白色门户,而不是只设置现有白色门户的白色门户值

您需要定义一个类门户

然后您可以在游戏对象中使用它,如下所示:

public GameObject {
    List<Portal> whitePortals;
    List<Portal> blackPortals;


    public GameObject() {
        whitePortals = new ArrayList<Portal>();
        blackPortals = new ArrayList<Portal>();
     }

    public GameObject addWhitePortal(int m, int o) throws Exception {
        whitePortals.add(new Portal(m, o));
        return this;
    }

    ...

    // You need to change other methods to follow a different data structure 
}

那么,您可以使用以下方法:

引入具有开始/结束属性的新门户类型 将类中的白色/黑色门户属性替换为白色和黑色门户或任何其他类型集合的列表 将getWhite/Black方法替换为访问列表 重构whitePortal和blackPortal方法以创建门户对象的新实例,并将它们添加到适当的集合中 当然,您可以使用数组而不是集合,但这有点麻烦

另外,假设门户是集合,您可能需要添加帮助器方法来操作这些集合。取决于你的实际需要

public class Portal
{
  private int start;
  private int end;

  public Portal(int start, int end) { ... }
  public getStart() {...}
  public getEnd() {...}
  public setStart(int end) {...}
  public setEnd(int start) {...}
}

public class GameObject
{
  ...
  private List<Portal> whitePortals = new ArrayList<Portal>();
  private List<Portal> blackPortals = new ArrayList<Portal>();
  ...

  public GameObject whitePortal(int m, int o) throws Exception {
    whitePortals.add(new Portal(m, o));
    return this;
  }

  public GameObject blackPortal(int o, int m) throws Exception {
    blackPortals.add(new Portal(m, o));
    return this;
  }
  ...
}

那么,您可以使用以下方法:

引入具有开始/结束属性的新门户类型 将类中的白色/黑色门户属性替换为白色和黑色门户或任何其他类型集合的列表 将getWhite/Black方法替换为访问列表 重构whitePortal和blackPortal方法以创建门户对象的新实例,并将它们添加到适当的集合中 当然,您可以使用数组而不是集合,但这有点麻烦

另外,假设门户是集合,您可能需要添加帮助器方法来操作这些集合。取决于你的实际需要

public class Portal
{
  private int start;
  private int end;

  public Portal(int start, int end) { ... }
  public getStart() {...}
  public getEnd() {...}
  public setStart(int end) {...}
  public setEnd(int start) {...}
}

public class GameObject
{
  ...
  private List<Portal> whitePortals = new ArrayList<Portal>();
  private List<Portal> blackPortals = new ArrayList<Portal>();
  ...

  public GameObject whitePortal(int m, int o) throws Exception {
    whitePortals.add(new Portal(m, o));
    return this;
  }

  public GameObject blackPortal(int o, int m) throws Exception {
    blackPortals.add(new Portal(m, o));
    return this;
  }
  ...
}