在java上迭代变量

在java上迭代变量,java,Java,我以随机方式增加每个变量,直到要分配的点的总和等于0。有没有什么聪明的方法可以减少方法generate()中的代码量,例如通过迭代变量而不是逐个列出它们 public class RandomStatsGenerator { int Strength = 3; int Dexterity = 3; int Constitution = 3; int Intelligence = 3; int Wisdom = 3; int Charisma =

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

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();
    }
}
公共类RandomStatsGenerator{
内强度=3;
智力灵巧度=3;
int构成=3;
智力智力=3;
智力=3;
内在魅力=3;
整数和=45;
静态最终int最大值=18;
public void generate(){

while(sum>0&&Strength您可以使用映射并在
while
循环中迭代一组条目:

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() + "."));
}

您可以使用映射并在
while
循环中迭代项集:

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() + "."));
}

可以使用简单数组和for循环:

public class RandomStatsGenerator {

    int[] attributes = {3, 3, 3, 3, 3, 3};
    int sum = 45;
    static final int MAX_VALUE = 18;

    public void generate() {
        for (int i = 0; sum != 0; i++) {
            boolean test = getRandomBoolean();
            if (test == true && attributes[i] <= MAX_VALUE) {
                attributes[i]++;
                sum--;
            }
            if ( i == 5 ) {
                i = -1;
            }
        }
        System.out.println("Strength: " + attributes[0] + ".");
        System.out.println("Dexterity: " + attributes[1] + ".");
        System.out.println("Constitution: " + attributes[2] + ".");
        System.out.println("Intelligence: " + attributes[3] + ".");
        System.out.println("Wisdom: " + attributes[4] + ".");
        System.out.println("Charisma: " + attributes[5] + ".");
    }
}
公共类RandomStatsGenerator{
int[]属性={3,3,3,3,3};
整数和=45;
静态最终int最大值=18;
public void generate(){
for(int i=0;sum!=0;i++){
布尔测试=getRandomBoolean();

如果(test==true&&attributes[i]您可以使用简单数组和for循环:

public class RandomStatsGenerator {

    int[] attributes = {3, 3, 3, 3, 3, 3};
    int sum = 45;
    static final int MAX_VALUE = 18;

    public void generate() {
        for (int i = 0; sum != 0; i++) {
            boolean test = getRandomBoolean();
            if (test == true && attributes[i] <= MAX_VALUE) {
                attributes[i]++;
                sum--;
            }
            if ( i == 5 ) {
                i = -1;
            }
        }
        System.out.println("Strength: " + attributes[0] + ".");
        System.out.println("Dexterity: " + attributes[1] + ".");
        System.out.println("Constitution: " + attributes[2] + ".");
        System.out.println("Intelligence: " + attributes[3] + ".");
        System.out.println("Wisdom: " + attributes[4] + ".");
        System.out.println("Charisma: " + attributes[5] + ".");
    }
}
公共类RandomStatsGenerator{
int[]属性={3,3,3,3,3};
整数和=45;
静态最终int最大值=18;
public void generate(){
for(int i=0;sum!=0;i++){
布尔测试=getRandomBoolean();

如果(test==true&&attributes[i]为属性创建枚举

public enum Attribute {
  STRENGTH,
  DEXTERITY,
  CONSTITUTION,
  INTELLIGENCE,
  WISDOM,
  CHARISMA;
}
然后,创建属性到其值的映射:

Map<Attribute, Integer> attributeValues = new HashMap<>();
attributeValues.put(Attribute.STRENGTH, 3);
attributeValues.put(Attribute.DEXTERITY, 3);
// ...
attributeValues.put(Attribute.CHARISMA, 3);
把它放在一个循环中,添加用于跟踪总和的代码,你就应该这样做了


另一方面,我不会检查每个迭代的所有属性,如果您想增加它,请使用随机布尔值检查每个属性,因为这有利于增加第一个属性。

为您的属性创建枚举

public enum Attribute {
  STRENGTH,
  DEXTERITY,
  CONSTITUTION,
  INTELLIGENCE,
  WISDOM,
  CHARISMA;
}
然后,创建属性到其值的映射:

Map<Attribute, Integer> attributeValues = new HashMap<>();
attributeValues.put(Attribute.STRENGTH, 3);
attributeValues.put(Attribute.DEXTERITY, 3);
// ...
attributeValues.put(Attribute.CHARISMA, 3);
把它放在一个循环中,添加用于跟踪总和的代码,你就应该这样做了


另一方面,我不会检查每次迭代的所有属性,如果您想增加它,请使用随机布尔值检查每个属性,因为这有利于增加第一个属性。

通过将类似代码提取到参数化方法,可以减少像您这样的代码重复

要做到这一点,我们需要稍微转换一下您的代码,以便将类似的代码转换为相同的代码:

public void generate(){

而(sum>0&&Strength通过将类似的代码提取到参数化方法,可以减少像您这样的代码重复

要做到这一点,我们需要稍微转换一下您的代码,以便将类似的代码转换为相同的代码:

public void generate(){

而(求和>0&&Strength)您只需生成一个随机数0-5并增加相应的变量(0->Strength,1->Dextrity)。重复此操作,直到总和等于所需的最大值。请遵循Java命名约定,并使用小写字母命名变量SunRelated,但所有要求使用数组的人都不正确:不要这样做。这些是字符类属性,带有名称、分隔逻辑和规则。请不要使用数组。正如我所说的我在回答中提到过,这样做将有利于检查序列顶部的属性,你应该选择一个(单个)随机数并增加它。你只需生成一个随机数0-5并增加相应的变量(0->强度,1->灵巧度)。重复此操作,直到总和等于所需的最大值。请遵循Java命名约定,并使用小写字母命名变量SunRelated,但所有要求使用数组的人都不正确:不要这样做。这些是字符类属性,带有名称、分隔逻辑和规则。请不要使用数组。正如我所说的我在回答中提到过,这样做有利于检查序列顶部的属性,你应该一个接一个地选择随机数并增加它。我会移除强度45@LaurentB我想展示一个解决OPs主要问题的通用方法。当然,这段代码还有很多需要改进的地方…谢谢你的输入。我刚刚开始学习Java,所以非常欢迎所有的反馈。我会去掉这些优点45@LaurentB我想展示一个通用的方法f或者OPs的主要问题。当然,这段代码还有很多需要改进的地方…谢谢你的输入。我刚刚开始学习Java,所以所有的反馈都是非常受欢迎的。是的,我承认它不漂亮,但它确实有效。我在循环结束时递增,所以在下一次迭代中再次设置为0。或者,你可以使用两个循环;一个代表i,一个代表sum。是的,我承认它不漂亮,但它确实有效。i在循环结束时递增,因此在下一次迭代中再次设置为0。或者,您可以使用两个循环:一个代表i,一个代表sum。
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;
}
  public void generate() {
        while (sum> 0 && Strength <= MAX_VALUE) {
            Strength = extracted(Strength);
            Dexterity = extracted(Dexterity);
            // same for the rest
    }