简体   繁体   English

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

[英]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);
    }
}

} win/loss types and values }赢/输类型和值

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.

相关问题 一个数字出现多少次 - How many times a number occurs 您如何在数组/字符串中找到特定值以及在数组/字符串中出现了多少次 - How do you find a specific value in an array/string and how many times it occurs in the array/string 如何对从数组输入的数字进行排序以判断输入了多少次 - How do I sort the numbers inputted from an array to tell how many times the number was entered 如何通过从文件中读取整数来找到整数出现的次数和最长的序列? - How do I find how many times an integer occurs and the longest sequence by reading it from a file? 如何执行密码检查以查看是否有数字? - How do i implement a check on the password to see if there is a number? 如何查找Java中的数组列表中特定元素出现的次数 - How to find the number of times a specific element occurs in an Array list in Java 比较两个文本文件,看看第二个文件中的单词在第一个文件中出现了多少次 - Compare two text files and see how many times the words in the 2nd file occurs in the 1st file 如何在数组中找到并打印在数组中恰好出现 K 次的最小数字,其中 K 是用户输入? - How can i find and print the smallest number in array which occurs exactly K times in an array, where K is an user input? 如何计算字符串出现的次数 - How To Count The Number of Times a String Occurs 我如何实现双和(无哈希 - 仅使用我的数组代码)? - How do I implement double sum (No Hash - Just using my Array code)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM