gpt4 book ai didi

Java 骰子游戏生成新数字时遇到问题

转载 作者:行者123 更新时间:2023-12-01 08:02:40 25 4
gpt4 key购买 nike

我正在尝试学习 OOP,从 Java 开始,因为我读过并被告知这是最好的起点。话虽如此,我正在尝试创建一个有趣的游戏来帮助我的学习,但我也知道游戏编程和设计可能更具挑战性。

所以我的目标是从 RollDice D20 生成新值,而无需重置或重新启动程序。您会注意到,当我打印出值时,我打印了同一个实例两次以演示我要避免的内容,并打印一个新实例来表明新实例确实生成了新值。也许,我没有以正确的方式处理这个问题,但这是我希望在一些帮助下克服的障碍!

我最终想要的是弄清楚如何生成一个新实例或至少生成一个新的滚动值,次数不限。非常感谢任何和所有的帮助!我添加了下面的代码作为示例。也欢迎任何其他反馈。

import java.util.Random;

class RollDice
{// Begin RollDice Class


// Initiate method rollDice
public static int rollDice(int number, int nSides)
{
// System.out.println( "--- Welcome to the Dice Game v2! ---" ); //
// welcomes player

Random r = new Random();
// Declare class variables
int num = 0;
int roll = 0;

if (nSides >= 3)
{
for (int i = 0; i < number; i++)
{
roll = r.nextInt(nSides) + 1;
// System.out.println("Roll is: " + roll);
num = num + roll;
}
}

else
{
System.out.println("Error num needs to be from 3");
}
return num;
} // end method rollDice

int d4 = rollDice(1, 4);
int d6 = rollDice(1, 6);
int d8 = rollDice(1, 8);
int d10 = rollDice(1, 10);
int d12 = rollDice(1, 12);
int d20 = rollDice(1, 20);

public RollDice()

{
this.d4 = d4;
}

public void setD4(int D4)
{
this.d4 = D4;
}

public int getD4()
{
return d4;
}

// ////////////////
{
this.d6 = d6;
}

public void setD6(int D6)
{
this.d6 = D6;
}

public int getD6()
{
return d6;
}

// ////////////////
{
this.d8 = d8;
}

public void setD8(int D8)
{
this.d8 = D8;
}

public int getD8()
{
return d8;
}

// ////////////////
{
this.d10 = d10;
}

public void setD10(int D10)
{
this.d10 = D10;
}

public int getD10()
{
return d10;
}

// ////////////////
{
this.d12 = d12;
}

public void setD12(int D12)
{
this.d12 = D12;
}

public int getD12()
{
return d12;
}

// ////////////////
{
this.d20 = d20;
}

public void setD20(int D20)
{
this.d20 = D20;
}

public int getD20()
{
return d20;
}
// ////////////////
}// End RollDice Class

class Champion
{// Begin Champion Class

RollDice champDice = new RollDice();
int champroll = champDice.getD20();

public Champion()
{
this.champroll = champroll;
}

public void setChampRoll(int ChampRoll)
{
this.champroll = ChampRoll;
}

public int getChampRoll()
{
return champroll;
}
}// End Champion Class

public class DiceRollTest
{
public static void main(String ars[])
{
Champion tChampion = new Champion();
Champion pChampion = new Champion();
System.out.println("Your Champion defends with a " + tChampion.getChampRoll() + "\n");
System.out.println("Your Champion defends with a " + tChampion.getChampRoll() + "\n");
System.out.println("Your Champion defends with a " + pChampion.getChampRoll() + "\n");
}
}

最佳答案

您的 RollDice 类没有实现您想要的效果。它所做的只是为您拥有的每种类型的骰子存储一个骰子掷骰结果。因此,当您对 Champion 对象调用 getChampRoll() 时,它所做的只是返回在构造 RollDice 类时已经发生的掷骰。

相反,您应该使 rollDice() 成为 RollDice 类的成员函数。然后,RollDice 应该在其构造函数中接受一个参数,指示调用 rollDice() 时应掷哪个骰子。这样,当您调用 getD20() 时,它将滚动 D20 并给出结果。

我将以此作为起点:

import java.util.Random;

public class Die {

private int mSides;
private Random mRandom;

public Die(int sides) {
this.mSides = sides;
mRandom = new Random(System.currentTimeMillis());
}

public int roll() {
return mRandom.nextInt(mSides + 1);
}

public int roll(int times) {
int sum = 0;
for (int i = 0; i < times; i++) {
sum += roll();
}
return sum;
}
}

您想要创建的每个骰子都可以继承/子类化该类。然后你可以把其中一些扔进你冠军的口袋里:)

关于Java 骰子游戏生成新数字时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24043611/

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