我在初学者的Java课堂上。我已经有3个星期的yahtzee计划了,但仍然无法解决。我需要两次掷5个骰子,以查看是否获得yahtzee(5个骰子相同),我在将骰子保存到第一个骰子中再次滚动时遇到麻烦。我确定有很多事情可以简化(如果转换为switch语句),但是现在我正在关注使这些方法起作用。

我们的老师为我们提供了一个模具课,可用于以下方面

public class Die
{
   private final int MAX = 6;  // maximum face value

   private int faceValue;  // current value showing on the die

   //-----------------------------------------------------------------
   //  Constructor: Sets the initial face value.
   //-----------------------------------------------------------------
   public Die()
   {
      faceValue = 1;
   }

   //-----------------------------------------------------------------
   //  Rolls the die and returns the result.
   //-----------------------------------------------------------------
   public int roll()
   {
      faceValue = (int)(Math.random() * MAX) + 1;

      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Face value mutator.
   //-----------------------------------------------------------------
   public void setFaceValue(int value)
   {
      faceValue = value;
   }

   //-----------------------------------------------------------------
   //  Face value accessor.
   //-----------------------------------------------------------------
   public int getVal()
   {
      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Returns a string representation of this die.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = Integer.toString(faceValue);

      return result;
   }
}


我的Yahtzee代码。

    import java.util.*;

public class Yahtzee
{

    int a, b, c, d, e;

    Die die1 = new Die();
    Die die3 = new Die();
    Die die4 = new Die();
    Die die5 = new Die();
    Die die2 = new Die();

    Scanner sc = new Scanner(System.in);

    ArrayList<Integer> dice2;

    int arrayLength;







    Die[] dice = new Die[5];

    //Constructor

    public Yahtzee()
    {


        for(int i = 0; i < dice.length; i ++)
        {
            dice[i] = new Die();
        }
    }

    public void roll()
    {
        for(int i = 0; i < dice.length; i ++)
        {
            dice[i].roll();
        }
    }


    public void saveDice()
    {
        dice2 = new ArrayList<Integer>();

        for(int i = 0; i < dice.length; i ++)
        {
            dice[i].getVal();
            for(int i2 = 0; i2 < dice.length; i2 ++)
            {
                if(i != i2)
                {
                    if(dice[i] == dice[i2])
                    {
                        dice2.add(dice[i].getVal());
                        dice2.add(dice[i2].getVal());
                         a = dice[i].getVal();


                         if(a == 5)
                        {
                            System.out.println("You have " + dice2.size() + "6's");
                        }
                         else if(a == 5)
                         {
                             System.out.println("You have " + dice2.size() + "5's");
                         }
                         else if(a == 4)
                         {
                             System.out.println("You have " + dice2.size() + "4's");
                         }
                         else if(a == 3)
                         {
                             System.out.println("You have " + dice2.size() + "3's");
                         }
                         else if(a == 2)
                         {
                             System.out.println("You have " + dice2.size() + "2's");
                         }
                         else if(a == 1)
                         {
                             System.out.println("You have " + dice2.size() + "1's");
                         }



                         b = dice2.size();
                    }


                    if(dice2.size() == 0)
                    {

                        if(a == 6)
                        {
                            System.out.println("No dice are the same. We kept 6 because its the largest face value.");

                        }
                        else if(a == 5)
                        {
                            System.out.println("No dice are the same. We kept 5 because its the largest face value.");
                        }
                        else if(a == 4)
                        {
                            System.out.println("No dice are the same. We kept 4 because its the largest face value.");

                        }
                        else if(a == 3)
                        {
                            System.out.println("No dice are the same. We kept 3 because its the largest face value.");

                        }
                        else if(a == 2)
                        {
                            System.out.println("No dice are the same. We kept 2 because its the largest face value.");

                        }
                        else if(a == 1)
                        {
                            System.out.println("No dice are the same. We kept 1 because its the largest face value.");

                        }
                    }

                    }

                }
        }
    }





    public void rollAgain()
    {
        arrayLength = dice2.size();
        System.out.println(arrayLength);


    }






}


我的再次掷骰方法不完整,因为我的保存骰子不起作用。

我的驱动程序如下,但到目前为止在输出中什么都不做,这是我的问题的一部分

public class YahtzeeFinal
{

    public static void main(String [] args)
    {
        Yahtzee yaht = new Yahtzee();

        yaht.roll();

        yaht.saveDice();

    }


}


感谢您的任何建议。

最佳答案

不知道您的代码是否正在运行,或者是否仍在运行,但这就是我想出的。我玩得很开心。您可能会注意到,我试图简化代码的某些部分,所以也许您会发现其中一些有用的方法:)如果您确实使用了其中的某些部分并遇到了问题,请告诉我,我会再看一遍。

import java.util.*;

public class Yahtzee {

    ArrayList<Die> savedDice = new ArrayList<Die>();

    // Constructor

    public Yahtzee() {
    }

    // renamed method to avoid confusion
    public void rollDice(int num) {
        Die[] dice = new Die[num];
        for (int d = 0; d < dice.length; d++) {
            Die nextDie = new Die();
            nextDie.roll();
            dice[d] = nextDie;
        }

        saveDice(dice);
    }

    public void saveDice(Die[] dice) {
        // int array will count occurrences of each face value
        int[] values = new int[] { 0, 0, 0, 0, 0, 0 };
        for (Die d : dice) {
            values[d.getVal() - 1]++;
        }
        int most = -1;
        int temp = 0;
        for (int j = 0; j < values.length; j++) {
            if (values[j] >= temp) {
                most = j + 1;
                temp = values[j];
            }
        }

        // Thought something like this looked much cleaner than having conditions for each value.
        if (temp > 1) {
            System.out.println("You have " + temp + " " + most + "'s");
        } else {
            System.out.println("No dice are the same. We kept " + most
                + " because it's the largest face value.");
        }

        for (Die d : dice) {
            if (d.getVal() == most) {
                savedDice.add(d);
            }
        }
    }

}

关于java - 编写yahtzee程序时遇到麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34258738/

10-11 19:01