- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的教科书给了我这段代码来帮助计算某个数字在整数数组中出现的次数。我尝试将教科书给我的代码应用到我的作业中,但它似乎不起作用。基本上,我必须在一个数组中生成 30 个随机整数,上限为 15,下限为 -5。
我想找出数组中的一个数字有多少次等于0、1、2...一直到10。第一个代码是我教科书给我的代码。他们还使用了随机数生成器,但他们想要查找每个数字出现的次数,而不是查找有多少个元素等于 0、1 等。 (分数数组只是随机数生成器,其上限为 100)。第二个代码是我的。
int[] counts = new int [100];
for (int i = 0; i < scores.length; i++) {
int index = scores[i];
counts[index]++;
}
//This is my code
public static void main(String[] args) {
int []a = arrayHist ();
printArray (a);
}
public static int randomInt (int low, int high) {
int range = (high - low) +1;
return (int) (Math.random() * range) + low;
}
public static int[] randomIntArray (int x) {
int[] random = new int[x];
for (int i = 0; i< x; i++) {
random [i] = randomInt (-5, 15);
}
return random;
}
public static int[] arrayHist () {
int[] counts = new int [30];
int[] hist = randomIntArray (30);
for (int i = 0; i < 10 && i >= 0; i++) {
int index = hist[i];
counts[index]++;
}
return hist;
}
public static void printArray (int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println (a[i]);
}
}
我应该只得到 11 个元素,但我又得到了 30 个随机数。这是为什么?
最佳答案
我会在你的代码中添加一些注释,看看你是否能发现哪里出了问题:
//take a histogram of the array. We're only going to count values between 0 and 10
//so 25th to 75 centiles, ignoring values that are lower than 0 or higher than 10
public static int[] arrayHist () {
//need to make an array of 11 numbers for the counts
int[] counts = new int [30];
//get an array of 30 random numbers
int[] hist = randomIntArray (30);
//loop over the whole array of 30 numbers
for (int i = 0; i < 10 && i >= 0; i++) {
//retrieve the random number into a variable temporarily
int index = hist[i];
//if the value is too low or too high, skip it
//else, store it in the counts array - the value from the random array
//serves as the index position in the counts array
counts[index]++;
}
//return the counts array
return hist;
}
我对评论所做的相当于使用你所思考的语言(英语)设计算法,然后你可以将其翻译成你正在学习的语言(java)。很少有开发人员用他们编写的编程语言进行思考。作为一名学生,我建议您在在注释下编写代码之前,应该始终编写注释来向自己解释您的算法。你会因为写注释而获得积分(通常),所以如果你先写注释,那么 a) 它可以帮助你编写代码,b) 在代码运行后你就不用再做繁琐的注释工作了
为了您自己的利益/学习,请在查看下面的剧透(答案)之前尝试找出上面的错误所在。将鼠标悬停在框上可显示剧透
//loop over the whole array of 30 numbers - YOU ONLY LOOP 10
for (int i = 0; i < 10 && i >= 0; i++) {
//if the value is too low or too high, skip it - YOU DIDN'T DO THIS CHECK
...
}
//return the counts array - YOU RETURNED THE WRONG ARRAY
return hist;针对评论进行的编辑:
检查范围
您必须检查两个限制,因此它需要采用以下形式之一:
if(x < 0 || x > 10) then don't do the count
if(!(x >= 0 && x <= 10)) then don't do the count
if(x >= 0 && x <= 10) then do the count
if(!(x < 0 || x > 10)) then do the count使用 NOT - 感叹号的测试
!
- 通常有点难以阅读和理解,因此可以尝试避免它们。 “积极的”测试 - 即它们返回积极的结果而不是需要否定的消极结果 - 更容易阅读和理解。在错误检查方面,循环和方法的一个有用提示是测试满足某些条件的错误值,如果遇到错误值,则跳过处理循环的其余部分(使用
continue
) 关键字,或跳过该方法的其余部分(通过return
从中获取)这样做意味着你的 if 主体( { 和 } 之间的位)不会变得很大。比较:
for(...){
if(test for bad values)
continue;
//50 lines long loop body
}比做更整洁:
for(...){
if(test for goodvalues){
//50 lines long loop body
}
}如果您使用底部模式,您可能会在几个 IF 之后陷入真正的缩进困惑,{ 和 } 遍布整个地方,并且您的代码会远远超出屏幕的右侧:
for(...){
//code
if(...){
//code
if(...){
//code
if(...){
//code
if(...){
//code
if(...){
//code
if(...){
//code
}
//code
}
//code
}
//code
}
//code
}
//code
}
//code
}将缩进级别保持在最低水平有助于提高代码的可读性
因此,我建议在您的情况下,不要测试 0 到 10 范围内的值并对其进行处理,而是采用“如果值超出 0 到 10 范围”的形式,跳过执行循环的其余部分
关于java - 这个方法在做什么? (数组和随机数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47329274/
我编写了一个函数来随机从 [-10,10] 中获取一对。 import System.Random main = do { s State g a randomSt = S
好的,我了解如何在 Scala 中实现随机数生成器以及如何设置生成的随机数的上限,但我对如何更改下限感到困惑。例如: var computerGuess= scala.util.Random
我写了一个函数来从 [-10,10] 中随机得到一对。 import System.Random main = do { s State g a randomSt = St
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在做一个项目,我需要在其中生成 8 个随机数。由于某种原因,我遇到随机数部分非常耗时的问题。 8 个随机数的意思是我需要一个由数字 0-9 组成的 8 个字符长的字符串。例如 01234567 或
这个问题已经有答案了: Why do I always get the same sequence of random numbers with rand()? (12 个回答) 已关闭 9 年前。
我看到这个问题可能已经在这里得到回答:Random using WELL512 但是,它对用户不太友好,也没有提供如何在“真实世界”的代码片段中使用它的示例。 这是我目前拥有的: #define m
我想知道是否有人可以为我澄清这一行。 Create a function die(x) which rolls a die x times keeping track of how many time
我正在制作一款有 6 名防守球员的足球比赛。我将这段代码设置为随机让他们都向四分卫移动。 我想知道是否有更好的方法来做到这一点。我知道必须有一种方法可以在没有这么多 if 语句的情况下循环它,但我对
在以下位置:http://www.fredosaurus.com/notes-cpp/misc/random.html 它提到如果我们想生成一个1-10范围内的随机数,我们可以这样做: r = (ra
如何在 Linux 和 C++ 中使用随机数? 我找到了一些我想使用的代码,它有一行 srand((unsigned)time(0));//seed 但是 gcc 说 board.cpp:94:24:
这个问题在这里已经有了答案: Generating random whole numbers in JavaScript in a specific range (40 个答案) 关闭 9 年前。
我有以下脚本: Timer=0; function countdown(auctionid){ var auctions; var divs; Timer=Timer+1;
利用oracle的dbms_random包结合rownum来实现,示例如下,随机取499户: select * from ( select * from busi.t_ar_
我需要获取随机数,但它不应该等于之前的数字。这是我的一段代码。但这不起作用。 function getNumber(){ var min = 0; var max = 4; var i;
我对 Haskell 还很陌生。我有一个数据类型: data Sentence= Prop Int | No Sentence | And [Sentence]
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
这个问题已经有答案了: How do I generate random integers within a specific range in Java? (73 个回答) 已关闭 7 年前。
function getRandomArbitrary(min, max) { var r = Math.floor(Math.random() * (max - min + 1) + m
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Generate random number with non-uniform density 我尝试识别/
我是一名优秀的程序员,十分优秀!