Java骰子游戏麻烦生成新数字

时间:2014-06-04 17:15:08

标签: java class object random instance

我正在努力学习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");
    }
}

2 个答案:

答案 0 :(得分:6)

您的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;
    }
}

可以为要创建的每个骰子继承/子类化此类。然后你可以在你的冠军口袋里扔掉其中一些:)

答案 1 :(得分:0)

首先,您应该创建新的构造函数。在这个构造函数中,你应该使用随机对象,并且可能有多少方面会骰子

class RollDice{
    Random rand;
    int sides;
    public RollDice(int x){
        sides = x;
        rand = new Random()
    }
}

然后,在该类中,您可以添加将生成新值的方法

public int roll(){
    return rand.nextInt(x);
}

您也可以生成具有固定值骰子边的RollDice对象,并使方法如下:

public int roll(int x){
    return rand.nextInt(x);
}

如果你希望java在控制台上输入东西后生成新值,只需使用循环与System.in.read()和if或switch语句(例如,如果有人输入1 - 生成新值,如果为0,则结束程序)

相关问题