gpt4 book ai didi

java - 编写 Yahtzee 程序时遇到问题

转载 作者:行者123 更新时间:2023-12-02 04:06:03 29 4
gpt4 key购买 nike

我正在参加初学者的java类(class)。我已经使用这个 yahtzee 程序三周了,但我仍然无法弄清楚这一点。我需要滚动 5 个骰子两次,看看我是否得到一个 yahtzee(5 个骰子相同) 我在第一次滚动时无法保存我的骰子以再次滚动 我的代码如下。我确信有很多事情可以简化(if 到 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com