首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代变量java

迭代变量java
EN

Stack Overflow用户
提问于 2018-07-10 19:59:44
回答 4查看 109关注 0票数 0

我以随机的方式递增每个变量,直到要分配的点数之和等于0。有没有什么聪明的方法可以减少generate()方法中的代码量,例如,通过迭代变量而不是逐个列出它们?

代码语言:javascript
复制
public class RandomStatsGenerator {

    int Strength = 3;
    int Dexterity = 3;
    int Constitution = 3;
    int Intelligence = 3;
    int Wisdom = 3;
    int Charisma = 3;
    int sum = 45;
    static final int MAX_VALUE = 18;

    public void generate() {
        while (sum> 0 && Strength <= MAX_VALUE) {
            boolean test = getRandomBoolean();
            if (test == true) {
                Strength++;
                sum--;
            }
            test = getRandomBoolean();
            if (test == true && Dexterity <= MAX_VALUE) {
                Dexterity++;
                sum--;
            }
            test = getRandomBoolean();
            if (test == true && Constitution <= MAX_VALUE) {
                Constitution++;
                sum--;
            }
            test = getRandomBoolean();
            if (test == true && Intelligence <= MAX_VALUE) {
                Intelligence++;
                sum--;
            }
            test = getRandomBoolean();
            if (test == true && Wisdom <= MAX_VALUE) {
                Wisdom++;
                sum--;
            }
            test = getRandomBoolean();
            if (test == true && Charisma <= MAX_VALUE) {
                Charisma++;
                sum--;
            }
        }
        System.out.println("Strength: " + Strength + ".");
        System.out.println("Dexterity: " + Dexterity + ".");
        System.out.println("Constitution: " + Constitution + ".");
        System.out.println("Intelligence: " + Intelligence + ".");
        System.out.println("Wisdom: " + Wisdom + ".");
        System.out.println("Charisma: " + Charisma + ".");
    }

    private boolean getRandomBoolean() {
        Random random = new Random();
        return random.nextBoolean();
    }
}
EN

回答 4

Stack Overflow用户

发布于 2018-07-10 20:25:55

通过提取与参数化方法类似的代码,可以减少类似的代码重复。

为了能够做到这一点,我们需要对你的代码进行一点转换,将相似的代码转换成相同的代码

代码语言:javascript
复制
public void generate() {
    while (sum> 0 && Strength <= MAX_VALUE) {
        int property = Strength;
        boolean test = getRandomBoolean();            
        if (test == true && property <= MAX_VALUE) { // added second condition to catch up with the following ifs
            property++;
            sum--;
        }
        Strength = property;

        property = Dexterity;
        test = getRandomBoolean();
        if (test == true && property <= MAX_VALUE) {
            property++;
            sum--;
        }
        Dexterity = property;
        // same for the rest

现在你有5行完全重复的代码:

代码语言:javascript
复制
        test = getRandomBoolean();
        if (test == true && property <= MAX_VALUE) {
            property++;
            sum--;
        }

您可以选择这5行中的第一行,并应用您的IDE提取方法重构。IDE将替换所选行的所有匹配项。

代码语言:javascript
复制
public void generate() {
    while (sum> 0 && Strength <= MAX_VALUE) {
        int property = Strength;
        property=extracted(property);
        Strength = property;

        property = Dexterity;
        property=extracted(property);
        Dexterity = property;
        // same for the rest
}
private int extracted(int property){
        test = getRandomBoolean();
        if (test == true && property <= MAX_VALUE) {
            property++;
            sum--;
        }
        return property;
}

当然,您应该在extaction过程中给新方法起一个更好的名字。我只是故意保留IDE建议的名称。

最后,您可以在原始方法中内联变量:

代码语言:javascript
复制
  public void generate() {
        while (sum> 0 && Strength <= MAX_VALUE) {
            Strength = extracted(Strength);
            Dexterity = extracted(Dexterity);
            // same for the rest
    }
票数 2
EN

Stack Overflow用户

发布于 2018-07-10 20:19:47

为您的属性创建枚举,

代码语言:javascript
复制
public enum Attribute {
  STRENGTH,
  DEXTERITY,
  CONSTITUTION,
  INTELLIGENCE,
  WISDOM,
  CHARISMA;
}

然后,创建属性到其值的映射:

代码语言:javascript
复制
Map<Attribute, Integer> attributeValues = new HashMap<>();
attributeValues.put(Attribute.STRENGTH, 3);
attributeValues.put(Attribute.DEXTERITY, 3);
// ...
attributeValues.put(Attribute.CHARISMA, 3);

从贴图中选取一个随机属性并增加它:

代码语言:javascript
复制
Random rand = new Random();
Attribute toIncrement = Attribute.values()[rand.nextInt(Attributes.values().length)];
attributeValues.compute(toIncrement, (k, v) -> v + 1);

把它放在一个循环中,添加跟踪总和的代码,你就应该完成了。

顺便说一句,我不会遍历每次迭代的所有属性,如果你想增加它,就用随机布尔值检查每个属性,因为这将有利于增加的第一个属性。

票数 1
EN

Stack Overflow用户

发布于 2018-07-10 20:13:03

您可以使用映射并迭代while循环中的条目集:

代码语言:javascript
复制
private Map<String, Integer> map = new HashMap<>();
{
    map.put("Strength", 3);
    map.put("Dexterity", 3);
    map.put("Constitution", 3);
    map.put("Intelligence", 3);
    map.put("Wisdom", 3);
    map.put("Charisma", 3);
}

int sum = 45;

public void generate() {
    while (sum > 0 && map.get("Strength") <= MAX_VALUE) {

        map.entrySet().forEach(entry -> {
            boolean test = getRandomBoolean();
            if (test) {
                map.merge(entry.getKey(), 1, (a, b) -> a + b);
                sum--;
            }
        });
    }

    map.entrySet().forEach(entry -> System.out.println(entry.getKey() + ": " 
       + entry.getValue() + "."));
}

如果所有变量的默认值都是3,您甚至可以使用列表进一步简化它:

代码语言:javascript
复制
List<String> keys = Arrays.asList("Stength", "Dexterity", 
        "Constitution", "Intelligence", "Wisdom", "Charisma");
private Map<String, Integer> map = new HashMap<>();

int sum = 45;

public void generate() {
    do {
        map.entrySet().forEach(entry -> {
            boolean test = getRandomBoolean();
            if (test) {
                map.merge(entry.getKey(), 3, (a, b) -> a + b);
                sum--;
            }
        });
    } while (sum > 0 && map.get("Strength") <= MAX_VALUE);

    //...rest
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51264631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档