[英]How do I implement an array to see how many times a number occurs in my code?
I have an assignment that “pulls” a slot machine 10,000 times and gives my results of how much money I won/lost.我的任务是“拉”一台老虎机 10,000 次,并给出我赢/输多少钱的结果。 How would I create an array to check the number of times I got a “loss” (0x), “free play” (1x), “double” (2x), “Mini jackpot” (100x), “Jackpot” (2000x your pay in)?
我将如何创建一个数组来检查我获得“损失”(0x)、“免费游戏”(1x)、“双倍”(2x)、“迷你头奖”(100x)、“头奖”(2000x)的次数你付钱)? I have tried everything I could to the best of my knowledge and just need some sort of direction on how to start.
据我所知,我已经尽我所能尝试了一切,只需要一些关于如何开始的方向。 Here is the code I have for the slot machine:
这是我的老虎机代码:
public class slotAssignment {
public static void main (String [] args) {
SlotMachine johnnysOneArmBandit = new SlotMachine();
//Pulls the slot machine with one quarter 10k times
double winnings = 0.0;
for( int i = 0; i <10000; i++) {
winnings = johnnysOneArmBandit.pull(1);
}
//"Pulls" the slot machine with two quarters 10k times
winnings = 0.0;
for(int i =0; i <10000; i ++) {
winnings = johnnysOneArmBandit.pull(2);
}
System.out.println("Spending: $5,000 + Winning: " + winnings);
//"Pulls" the slot machine with three quarters 10k times
winnings = 0.0;
for(int i =0; i <10000; i ++) {
winnings = johnnysOneArmBandit.pull(3);
System.out.println("Spending: $7,500 + Winning: " + winnings);
}
}
First of all, I believe winnings = johnnysOneArmBandit.pull(#) should be winnings += johnnysOneArmBandit.pull(#).首先,我认为winners = johnnysOneArmBandit.pull(#) 应该是winners += johnnysOneArmBandit.pull(#)。 I don't know this for a fact because you didn't provide the method pull().
我不知道这一点,因为您没有提供方法 pull()。
As for checking how many times you got each of those, you can check during each iteration of the loop what the result was.至于检查你得到了多少次,你可以在循环的每次迭代期间检查结果是什么。 To store the number of times of each occurrence, the easiest way is probably an array of five doubles.
要存储每次出现的次数,最简单的方法可能是一个包含五个双精度数的数组。
For example, if you want to check for a mini jackpot:例如,如果您想检查迷你头奖:
// For our purposes, 0 is loss, 1 is free play, 2 is double, 3 is mini jackpot, and 4 is jackpot
double[] count = {0,0,0,0,0};
for( int i = 0; i <10000; i++) {
double x = johnnysOneArmBandit.pull(1);
if(x == 0.25 * 100) {
count[3]++;
}
// etc.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.