java老虎机代码OOP VS程序

时间:2015-11-10 22:17:21

标签: java

嘿伙计们,所以我写了这段代码,看看marge在仅使用slotmachine的文本中耗尽了多长时间。所以它的工作方式是程序性的。 (完美无瑕) 她按照一,二,三的顺序玩机器1 2 3。

我为同样的事情编写了OOP代码,但由于某种原因,在这种情况下硬币var或桶继续走向负,也就是0 -1 -2。我输入简单的代码停止在0但它转到-2。

这是有效的代码。

public class Coins {

    public static void main(String[] args) {
        runSlots(5000, 30, 10, 9);
    }

    public static void runSlots(int totalCoins, int firstStep, int secondStep, int thirdStep) {
        int coins = totalCoins;
        int timePlayed = 0;
        int machineOneStep = firstStep, machineTwoStep = secondStep, machineThreeStep = thirdStep;
        System.out.println("How many quarters does Marge have in the jar? " + totalCoins);
        System.out.println("How many times has the first machine been played since paying a jackpot?" + machineOneStep);
        System.out.println("How many times has the second machine been played since paying a jackpot?" + machineTwoStep);
        System.out.println("How many times has the third machine been played since paying a jackpot?" + machineThreeStep);

        while (coins != 0) { //machine one
            coins -= 1;
            machineOneStep += 1;
            timePlayed += 1;
            //machine two
            coins -= 1;
            machineTwoStep += 1;
            timePlayed += 1;
            //machine three
            coins -= 1;
            timePlayed += 1;
            machineThreeStep += 1;
            if (machineOneStep == 40) {
                machineOneStep = 1;
                coins += 30;
            }
            if (machineTwoStep == 85) {
                machineTwoStep = 1;
                coins += 60;
            }
            if (machineThreeStep == 10) {
                machineThreeStep = 1;
                coins += 11;
            }
        }
        System.out.println("Marge played " + timePlayed + " times, but has run out of money.");
    }
}

这是OOP代码

public class SlotMachine {

    private int machineNumber, payout, currentStep, winningStep;

    public SlotMachine(int mN, int pO, int s, int wS) {
        this.machineNumber = mN;
        this.payout = pO;
        this.currentStep = s;
        this.winningStep = wS;
    }

    public int getWinningStep() {
        return this.winningStep;
    }
    public void setWinningStep(int winningStep) {
        this.winningStep = winningStep;
    }

    public int getMachineNumber() {
        return this.machineNumber;
    }
    public void setMachineNumber(int machineNumber) {
        this.machineNumber = machineNumber;
    }

    public int getPayout() {
        return this.payout;
    }
    public void setPayout(int payout) {
        this.payout = payout;
    }

    public int getCurrentStep() {
        return this.currentStep;
    }
    public void setCurrentStep(int currentStep) {
        this.currentStep = currentStep;
    }

    public int play() {
        boolean isWinner = false;
        int amountWon = -1;
        this.currentStep = getCurrentStep() + 1;
        if (getCurrentStep() == getWinningStep()) {
            isWinner = true;
            setCurrentStep(1);
            amountWon = getPayout();
            return amountWon;
        } else
            return amountWon;
    }

    @Override
    public String toString() {
        return "" + "Slot machine number " + machineNumber + " payout is " + payout + " and  currentStep is " + currentStep;
    }
}

和主要方法

public static void main(String[] args) {
    int bucket = 5000;
    int timePlayed = 0;
    SlotMachine slotOne = new SlotMachine(1, 30, 17, 40);
    SlotMachine slotTwo = new SlotMachine(2, 60, 72, 85);
    SlotMachine slotThree = new SlotMachine(3, 11, 6, 10);
    System.out.println("How many quarters does Martha have in the bucket? " + bucket);
    while (bucket != 0) {

        //bucket-=1;
        bucket += slotOne.play();
        timePlayed += 1;
        System.out.println(bucket);
        //bucket-=1;
        bucket += slotTwo.play();
        timePlayed += 1;
        System.out.println(bucket);
        //bucket-=1;
        bucket += slotThree.play();
        timePlayed += 1;
        System.out.println(bucket);
        //failsafe
        if (bucket <= 0) {
            bucket = 0;
        }
    }
    System.out.println("Margerie plays " + timePlayed + " times.");
}

2 个答案:

答案 0 :(得分:0)

那是因为你没有测试&#34;硬币 - = 1&#34;的每个事件,你有一段时间,如果硬币是0,你在测试之前减去三次。 / p>

例如: 如果您的硬币事件从值1开始,您将减去它三次导致数字-2并且永不停止,具体取决于您输入的步骤。

试试这个:

    public static void runSlots(int totalCoins,int firstStep,int secondStep, int thirdStep){
         int coins = totalCoins;
         int timePlayed = 0;
         int machineOneStep = firstStep ,machineTwoStep = secondStep , machineThreeStep = thirdStep;
            System.out.println("How many quarters does Marge have in the jar? " + totalCoins);
            System.out.println("How many times has the first machine been played since paying a jackpot?" + machineOneStep);
            System.out.println("How many times has the second machine been played since paying a jackpot?" + machineTwoStep);
            System.out.println("How many times has the third machine been played since paying a jackpot?" + machineThreeStep);

        while (coins !=0)
        {   
                //machine one
                coins -= 1;
                machineOneStep += 1;
                timePlayed +=1;
                if (machineOneStep == 40){
                    machineOneStep = 1;
                    coins += 30;
                }
                //machine two
                if (coins !=0) {
                   coins -= 1;
                   machineTwoStep += 1;
                   timePlayed +=1;
                   if (machineTwoStep == 85){
                      machineTwoStep = 1;
                      coins += 60;
                   }
                }
                //machine three
                if (coins !=0) {
                   coins -= 1;
                   timePlayed +=1;
                   machineThreeStep += 1;
                   if (machineThreeStep == 10){
                      machineThreeStep = 1;
                      coins += 11;
                   }
                }
            }
}

答案 1 :(得分:0)

是的,当用户获胜时我在SlotMachine课程中找到了它我没有收费,所以我不得不拿下赢 - 1;赢得部分代码。它解决了。