繁体   English   中英

如何实现一个数组来查看一个数字在我的代码中出现了多少次?

[英]How do I implement an array to see how many times a number occurs in my code?

我的任务是“拉”一台老虎机 10,000 次,并给出我赢/输多少钱的结果。 我将如何创建一个数组来检查我获得“损失”(0x)、“免费游戏”(1x)、“双倍”(2x)、“迷你头奖”(100x)、“头奖”(2000x)的次数你付钱)? 据我所知,我已经尽我所能尝试了一切,只需要一些关于如何开始的方向。 这是我的老虎机代码:

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);
    }
}

}赢/输类型和值

首先,我认为winners = johnnysOneArmBandit.pull(#) 应该是winners += johnnysOneArmBandit.pull(#)。 我不知道这一点,因为您没有提供方法 pull()。

至于检查你得到了多少次,你可以在循环的每次迭代期间检查结果是什么。 要存储每次出现的次数,最简单的方法可能是一个包含五个双精度数的数组。

例如,如果您想检查迷你头奖:

// 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.

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