有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

所以在我的main方法中,我像create一样调用it,像create一样调用这个类

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

我的问题是当我想添加多个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。我如何设置它,使我可以有多个黑白门户


共 (2) 个答案

  1. # 1 楼答案

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

    1. 引入一种具有开始/结束属性的新“门户”类型
    2. 将类中的白色/黑色门户属性替换为白色和黑色门户(或您喜欢的任何其他类型的集合)的列表
    3. 将getWhite/Black方法替换为访问列表
    4. 重构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;
      }
      ...
    }
    
  2. # 2 楼答案

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

    您需要定义一组白色门户和一组黑色门户。如果这样做,调用方法whitePortal(5, 12)添加一个新的白色门户,而不是设置现有唯一白色门户的白色门户值

    您需要定义一个类Portal

    public class Portal {
        private int portalStart;
        private int portalEnd;
        ...
    
        public Portal(int s, int e) {
            this.portalStart = s;
            this.portalEnd = e;
        }
    }
    

    然后您可以在GameObject中使用它,如下所示:

    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 
    }