【问题标题】:Changing variable based on loop sequence in java在java中根据循环序列更改变量
【发布时间】:2016-02-26 23:18:43
【问题描述】:

所以我在玫瑰游戏中构建花瓣并将骰子定义为 dice1、dice 2 等。然后我运行一个循环来确定每个骰子的值是多少,并将其添加到游戏值中。我想要的是循环的每个序列都可以切换它正在查看的变量。所以首先运行看看 dice1。然后再看 dice2。

public class Rosegame
{
    private int dice1;
    private int dice2;
    private int dice3;
    private int dice4;
    private int dice5;
    private int gameValue;

    public void rollDice()
    {
       dice1 = (int) ((6-1+1) * Math.random()) + 1;
       dice2 = (int) ((6-1+1) * Math.random()) + 1;
       dice3 = (int) ((6-1+1) * Math.random()) + 1;
       dice4 = (int) ((6-1+1) * Math.random()) + 1;
       dice5 = (int) ((6-1+1) * Math.random()) + 1;

    }

    public void printValues()
    {
        System.out.println("Dice 1 is:" + dice1);
        System.out.println("Dice 2 is:" + dice2);
        System.out.println("Dice 3 is:" + dice3);
        System.out.println("Dice 4 is:" + dice4);
        System.out.println("Dice 5 is:" + dice5);

    }

    public int calculatePetalsOnRose()
    {
        gameValue = 0;
        for(int i = 1; i <=5; i++)
        {
            if (dice1 == 5)
            {
                gameValue = gameValue + 4;
            }
            else if (dice1 == 3)
            {
                gameValue = gameValue + 2;
            }
            else
            {
                gameValue = gameValue;
            }
        }
        return gameValue;
    }
}

这是我当前的代码,我需要的是在它有 dice1 的 if 语句中,我希望它能够在每次循环时更改。他们也是一个运行方法并允许输入的驱动程序,但到目前为止它正在工作。提前非常感谢您

编辑:

将变量切换到我现在拥有的数组,这给了我一个错误,说 java.lang.NullPointerException。它发生在调用 rollDice 函数时。

public class PetalsGame
{
    private int[] anArrayDice;
    private int gameValue;

    public void rollDice()
    {
       anArrayDice[0] = (int) ((6-1+1) * Math.random()) + 1;
       anArrayDice[1] = (int) ((6-1+1) * Math.random()) + 1;
       anArrayDice[2] = (int) ((6-1+1) * Math.random()) + 1;
       anArrayDice[3] = (int) ((6-1+1) * Math.random()) + 1;
       anArrayDice[4] = (int) ((6-1+1) * Math.random()) + 1;


    }

    public void printDice()
    {
        System.out.println("Dice 1 is:" + anArrayDice[0]);
        System.out.println("Dice 2 is:" + anArrayDice[1]);
        System.out.println("Dice 3 is:" + anArrayDice[2]);
        System.out.println("Dice 4 is:" + anArrayDice[3]);
        System.out.println("Dice 5 is:" + anArrayDice[4]);

    }

    public int calculateAllPetals()
    {
        gameValue = 0;
        for(int i = 0; i <=4; i++)
        {
            if (anArrayDice[i] == 5)
            {
                gameValue = gameValue + 4;
            }
            else if (anArrayDice[i] == 3)
            {
                gameValue = gameValue + 2;
            }
            else
            {
                gameValue = gameValue;
            }
        }
        return gameValue;
    }
}

我可以通过添加 anArrayDice = new int[5]; 来修复它在 rollDice 方法内部。谢谢你的帮助。

【问题讨论】:

  • 我将变量切换到私有 int[] 骰子;然后使用相同的 math.random 定义骰子值。我有线 dice[0] = (int) ((6-1+1) * Math.random()) + 1;在运行时给我一个错误。在底部的 bluej 终端中显示 java.lang.nullpointerexception。我对数组不了解的地方?
  • 您能否更新您的问题,以便清楚您尝试了什么?

标签: java


【解决方案1】:

您似乎只掷了一个骰子(至少这是您似乎感兴趣的唯一值),所以我将从提取一种掷骰子的方法开始。为了便于阅读,我也更喜欢Random.nextInt(int)。类似的,

private static final Random rand = new Random();

private static int rollDie(int sides) {
    return rand.nextInt(sides) + 1;
}

然后您可以根据需要使用该方法滚动并计算您的玫瑰。此外,您可以使用x += y; 代替x = x + y;。类似的,

public int calculatePetalsOnRose() {
    int gameValue = 0;
    for (int i = 1; i <= 5; i++) {
        int dice = rollDie(6);
        System.out.printf("Dice %d is: %d%n", i, dice);
        if (dice == 5) {
            gameValue += 4;
        } else if (dice == 3) {
            gameValue += 2;
        }
    }
    return gameValue;
}

【讨论】:

    【解决方案2】:

    我认为如果你使用数组来存储你的骰子,这样做会容易得多。它还使您不必一遍又一遍地重复相同的代码行。

    public int [] diceArray;
    private int gameValue;
    
    public void rollDice()
    {
        for(int i =0; i < diceArray.length; i++)
        {
            diceArray[i] = 6 *((int)Math.random()+ 1);
        }
    
    }
    
    public void printValues()
    {
        for(int i=0; i < diceArray.length; i++)
        {
            System.out.println("Dice" + (i+1) +"is:" + diceArray[i]);
        }
    }
    
    public int calculatePetalsOnRose()
    {
        gameValue = 0;
        for(int i = 0; i < diceArray.length; i++)
        {
            if(diceArray[i] == 5)
            {
                gameValue += 4;
            }
            else if (diceArray[i] == 3)
            {
                gameValue +=2;
            }
        }
    
        return gameValue;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-05
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多