我一直在研究我的java类的问题,我们必须创建3个老虎机对象并播放它们,直到我们用完了四分之一。第一台机器每40次付30个季度就玩了30次。第二台机器每85次播放60次,播放次数为10次,第三台机器每播放10次,播放次数为11次,播放次数为9次。然后我计算了机器播放的次数,直到它们没钱了。在我运行该程序之后,它说它播放机器18770次,但是,我将它与我的其他同学进行比较,并且它们的输出在每个值33569时是不同的。这是我的代码:
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");
}
}
这是第二堂课:
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;
}
}
这是我的输出:
罐子里有多少个季度:
5000
第一台机器播放了多少次:
30
第二台机器播放了多少次:
10
第三台机器播放了多少次:
9
玛奇总共打了18770次由于播放的次数是18770,我想知道它为什么会这样,为什么/如何使输出等于33569。
答案 0 :(得分:1)
在循环的每次迭代中,您正在每台机器播放两次
game2.game();
quarters += game2.game();
当您调整四分之一并为其中一个增加游戏时。