简体   繁体   English

老虎机程序错误输出

[英]Slot machine program incorrect output

I've been working on a problem for my java class where we must create 3 slot machine objects and play them until we have run out of quarters.我一直在为我的 java 类解决一个问题,我们必须创建 3 个老虎机对象并播放它们直到我们用完四分之一。 the first machine pay 30 quarters every 40th time it was play which has been played 30 times.第一台机器每播放 40 次支付 30 个季度,已播放 30 次。 The second machine pays out 60 quarters every 85th time it was played which was played 10 times and the third machine pays out 11 quarters every 10th time it was played which has been played 9 times.第二台机器每播放 85 次支付 60 个四分之一,播放了 10 次,第三台机器每播放 10 次支付 11 个四分之一,已经播放了 9 次。 I've then counted the amount of times the machines have been played until they have run out of money.然后我计算了机器玩到钱用完为止的次数。 After i run the program, it says it played the machines 18770 times, however, i compared it to my other classmates and their output are different at a value of 33569 for each of them.在我运行程序后,它说它播放了机器 18770 次,但是,我将它与我的其他同学进行了比较,他们每个人的输出值都不同,为 33569。 Here's my code:这是我的代码:

 import java.util.Scanner;
 public class Slots {
 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Slotmachines game1 = new Slotmachines(30, 40);
    Slotmachines game2 = new Slotmachines(60, 85);
    Slotmachines game3 = new Slotmachines(11, 10);
    int quarters;
    int plays = 0;
    int play1, play2, play3;
    System.out.println("How many quarters are in the jar: \n");
    quarters = input.nextInt();
    System.out.println("How many times has the first machine been played: \n");
    play1 = input.nextInt();
    play1 = game1.setCounter(play1);
    System.out.println("How many times has the second machine been played: \n");
    play2 = input.nextInt();
    play2 = game2.setCounter(play2);
    System.out.println("How many times has the third machine been played: \n");
    play3 = input.nextInt();
    play3 = game3.setCounter(play3);
    while(quarters != 0){
        plays++;
        quarters--;
        game1.game();
        quarters += game1.game();
        if(quarters != 0){
            plays++;
            quarters--;
            game2.game();
            quarters += game2.game();
        }
        if(quarters != 0){
            plays++;
            quarters--;
            game3.game();
            quarters += game3.game();
        }
    }
    System.out.println("Marge played a total of " + plays + " times");



}

} }

and here's the second class:这是第二堂课:

 public class Slotmachines {
    int payOut;
   int playLimit;
   int counter;



   public Slotmachines(int payOut, int playLimit) {
       this.payOut = payOut;
       this.playLimit = playLimit;
   }


   public void setPayOut(int payOut) {
       this.payOut = payOut;
   }

   public int getPayOut() {
       return payOut;
   }

   public void setPlayLimit(int playLimit) {
       this.playLimit = playLimit;
   }

   public int getPlayLimit() {
       return playLimit;
   }

   public int setCounter(int counter){
       this.counter = counter;
       return this.counter;
   }

   public int getSlotCounter() {
       return counter;
   }


   public int game() {
       int result = 0;
       counter++;
       if (counter >= playLimit) {
           counter = 0;
           result = payOut;
       }
       return result;
}
}

Here's my output:这是我的输出:

How many quarters are in the jar:罐子里有多少个四分之一:

5000 5000

How many times has the first machine been played:第一台机器玩了多少次:

30 30

How many times has the second machine been played:第二台机器玩了多少次:

10 10

How many times has the third machine been played:第三台机器玩了多少次:

9 9

Marge played a total of 18770 times Marge 总共玩了 18770 次

Since the amount of times played is 18770, I would like to know why it's coming like this and why/how i could make the output equal to 33569.由于播放的次数是 18770,我想知道为什么会这样以及为什么/如何使输出等于 33569。

You are playing each machine twice in each iteration of the loop在循环的每次迭代中,您都在每台机器上播放两次

game2.game();
quarters += game2.game();

While you are adjusting quarters and incrementing play for only one of them.当您只为其中一个调整季度和增加游戏时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM