我正在尝试学习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;
}
}
可以为要创建的每个骰子继承/子类化此类。然后,您可以将其中一些扔进冠军的口袋里:)